/**
 * Modify History:
 *  NO.01    Lin.Xin     2014/12/15  Fix 4C00 仓库返工工艺路线变更为问题.   
 */
package com.service;

//import java.sql.Date;
import static com.def.GenericDef.BANK_OUT;
import static com.def.GenericDef.DEST_SHOP_BANK_FLG_NO_WIP;
import static com.def.GenericDef.DEST_SHOP_BANK_FLG_WIP;
import static com.def.GenericDef.EVT_OUT_RETURN;
import static com.def.GenericDef.EVT_WHCG;
import static com.def.GenericDef.EVT_WHIN;
import static com.def.GenericDef.EVT_WHOUT;
import static com.def.GenericDef.EVT_WH_RETURN;
import static com.def.GenericDef.EVT_WIP_WHIN;
import static com.def.GenericDef.EVT_WIP_WHIO;
import static com.def.GenericDef.E_ADD_EXIST;
import static com.def.GenericDef.E_BIS_DATA;
import static com.def.GenericDef.E_BIS_OPE;
import static com.def.GenericDef.E_BIS_PATH;
import static com.def.GenericDef.E_MPWMSOPE_INVALID_QTY;
import static com.def.GenericDef.E_MPWMSOPE_INVALID_STAT;
import static com.def.GenericDef.E_MPWMSOPE_NOT_FOUND_PATH;
import static com.def.GenericDef.E_MPWMSOPE_NOT_FOUND_START_OPE;
import static com.def.GenericDef.E_READ_NOT_FOUND;
import static com.def.GenericDef.E_RET_BOX;
import static com.def.GenericDef.E_RET_PRD_IN;
import static com.def.GenericDef.E_RET_PRD_INFO;
import static com.def.GenericDef.E_RET_SHIP_NOTICE;
import static com.def.GenericDef.E_RET_WO;
import static com.def.GenericDef.E_XPAPLYWO_CLOSE_STAT;
import static com.def.GenericDef.E_XPWMSOPE_ADMIN_FLG;
import static com.def.GenericDef.E_XPWMSOPE_DN_NOT_ENOUGH;
import static com.def.GenericDef.E_XPWMSOPE_FIND_NO_PANEL_BY_BOX;
import static com.def.GenericDef.E_XPWMSOPE_INVALID_INPUT;
import static com.def.GenericDef.E_XPWMSOPE_INVALID_WO_MDL_CATE;
import static com.def.GenericDef.E_XPWMSOPE_IN_WIP_BANK_SHIPPING_FLG_INVALIED;
import static com.def.GenericDef.E_XPWMSOPE_SAME_DEST_SHOP;
import static com.def.GenericDef.E_XPWMSOPE_SAME_WO;
import static com.def.GenericDef.E_XPWMSOPE_SHIP_NOTICE_COMP;
import static com.def.GenericDef.E_XPWMSOPE_SHIP_NOTICE_HOLD;
import static com.def.GenericDef.E_XPWMSOPE_SHIP_NOTICE_NOT_WAIT;
import static com.def.GenericDef.E_XPWMSOPE_WO_CNT_NOT_ENOUGH;
import static com.def.GenericDef.E_XPWMSOPE_WO_UNMATCH;
import static com.def.GenericDef.GROUP_ID_MIX;
import static com.def.GenericDef.INVALIED_BOX_STAT;
import static com.def.GenericDef.INVALIED_SHT_CNT;
import static com.def.GenericDef.INVALIED_SHT_STAT;
import static com.def.GenericDef.MDL_CATE_DM;
import static com.def.GenericDef.MDL_CATE_JB;
import static com.def.GenericDef.MDL_CATE_QG;
import static com.def.GenericDef.PATH_FRST_OPE;
import static com.def.GenericDef.PRD_GRADE_MIX;
import static com.def.GenericDef.PROC_COMPLETE;
import static com.def.GenericDef.PROC_EMPTY;
import static com.def.GenericDef.PROC_HOLD;
import static com.def.GenericDef.PROC_SHIP;
import static com.def.GenericDef.PROC_SHTC;
import static com.def.GenericDef.PROC_WAIT;
import static com.def.GenericDef.PROC_WFRL;
import static com.def.GenericDef.RETURN_CODE_OK;
import static com.def.GenericDef.RETURN_CODE_SET_ERROR;
import static com.def.GenericDef.RETURN_CODE_UNKNOWN;
import static com.def.GenericDef.RETURN_MESG_OK;
import static com.def.GenericDef.TRX_IN;
import static com.def.GenericDef.TRX_OUT;
import static com.def.GenericDef.T_AMSLZGRADE;
import static com.def.GenericDef.T_AMSNGGRADE;
import static com.def.GenericDef.T_XPWMSOPE;
import static com.def.GenericDef.WO_STATUS_CLOS;
import static com.def.GenericDef.WO_STATUS_WAIT;
import static com.def.GenericDef._NO;
import static com.def.GenericDef._NO1_50;
import static com.def.GenericDef._NORMAL;
import static com.def.GenericDef._SPACE;
import static com.def.GenericDef._YES;

import java.sql.Timestamp;
import java.text.ParseException;
import java.util.*;
import java.util.Map.Entry;

import javax.annotation.Resource;

import com.dao.*;
import com.model.*;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.icim.service.ICIMBaseService;
import com.rtnBean.NotFoundRtnBeen;
import com.rtnBean.SimpleRtnBeen;
import com.tx.Amsgrade.AmslzgradeI;
import com.tx.Amsgrade.AmsnggradeI;
import com.tx.Csotshipping.CsotShippingO;
import com.tx.Xpwmsope.XpwmsopeI;
import com.tx.Xpwmsope.XpwmsopeIA;
import com.tx.Xpwmsope.XpwmsopeIB;
import com.tx.Xpwmsope.XpwmsopeIC;
import com.tx.Xpwmsope.XpwmsopeO;
import com.tx.Xpwmsope.XpwmsopeOA;
import com.util.GUIDGenerator;
import com.util.HttpClientUtil;
import com.util.MqOpe;
import com.util.Util;

@Scope("prototype")
@Service("XpwmsopeService")
public class XpwmsopeService extends ICIMBaseService{
	private static Logger logger = Logger.getLogger(XpwmsopeService.class);
	private static String TX_NAME = T_XPWMSOPE;

	@Autowired
	private XpwmsopeI inTrx;
	@Autowired
	private XpwmsopeO outTrx;
	@Autowired
	private IRetPrdInDao retPrdInDao;
	@Autowired
	private IRetBoxDao retBoxDao;
	@Autowired
	private IRetPrdInfoDao retPrdInfoDao;
	@Autowired
	private IRetWODao retWODao;
	@Autowired
	private IBisPathDao bisPathDao;
	@Autowired
	private IRetShipNoticeDao retShipNoticeDao;
	@Autowired
	private IRetBoxShtcDao retBoxShtcDao;
	@Autowired
	private IRetPrdInfoShtcDao retPrdInfoShtcDao;
	@Autowired
	private IBisOpeDao bisOpeDao;
	@Autowired
	private IBisDataDao bisDataDao;
	@Autowired
	private IRetCrtNoDao retCrtNoDao;
	@Autowired
	private SendMsgService sendMsgService;
	@Autowired
	private HttpClientUtil  httpClientUtil;
	@Autowired
	private MqOpe mqOpe;
	@Autowired
	private IBisMdlDefDao bisMdlDefDao;
	@Resource
	private HttpClientUtil xmtmHttpClientUtil;
	
	@Resource
	private HttpClientUtil xmtm5HttpClientUtil;

	@Resource
	private HttpClientUtil  csotHttpClientUtil;
	
	private String gszMsgBuf = "";
	private Timestamp gEvtTimestamp;
	private Double gEvtTimeD;
	private String gEvtSeqID;

	public String subMainProc(String strInMsg) {
		Session session = retPrdInfoDao.getCrSession();
		long rtnCode = _NORMAL;
		String outMsg = null;
		gszMsgBuf = "";
		gEvtTimestamp = Util.getCurrentTimestamp();
		gEvtTimeD = Util.getDoubleTime();
		gEvtSeqID = GUIDGenerator.javaGUID();
		outTrx.setTrx_id(TX_NAME);
		outTrx.setRtn_code(String.valueOf(RETURN_CODE_SET_ERROR));
		outTrx.setType_id(TRX_OUT);

		try {
			inTrx = (XpwmsopeI) mqOpe.inStr2Obj(strInMsg, inTrx);
			logger.info("InTrx is " + strInMsg);

			rtnCode = subMainProc2(inTrx.getAction_flg().toCharArray()[0]);
			if (rtnCode != _NORMAL) {
				TransactionAspectSupport.currentTransactionStatus()
						.setRollbackOnly();
				outTrx.setRtn_code(String.valueOf(rtnCode));
				outTrx.setRtn_mesg(gszMsgBuf);
			} else {
				outTrx.setRtn_code(String.valueOf(RETURN_CODE_OK));
				outTrx.setRtn_mesg(RETURN_MESG_OK);
				session.flush();
			}
		} catch (Exception ex) {
			TransactionAspectSupport.currentTransactionStatus()
					.setRollbackOnly();
			outTrx.setRtn_mesg(ex.toString());
			outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
			logger.error(Util.stackTraceToString(ex));
		} finally {
			outMsg = mqOpe.obj2Str(outTrx);
			return outMsg;
		}
	}

	/**
	 * Real action type
	 * 
	 * @param actionType
	 * @return
	 * @throws ParseException 
	 */
	private long subMainProc2(char actionType) throws ParseException {
		long lRc = _NORMAL;
		switch (actionType) {
			case 'Q': {
				lRc = getWhsInfo();
				if (lRc != _NORMAL) {
					return lRc;
				}
				break;
			}
			case 'B': {
				lRc = getWhsInfo2();
				if (lRc != _NORMAL) {
					return lRc;
				}
				break;
			}
			case 'I': {
				lRc = whIn();
				if (lRc != _NORMAL) {
					return lRc;
				}
				break;
			}
			case 'W': { // WIP dest shop back
				lRc = wipBack();
				if (lRc != _NORMAL) {
					return lRc;
				}
				break;
			}
			case 'C': {
				lRc = whChg();
				if (lRc != _NORMAL) {
					return lRc;
				}
				break;
			}
			case 'O': {
				lRc = whOut();
				if (lRc != _NORMAL) {
					return lRc;
				}
				break;
			}
			case 'R': {// 仓库在库返回产线
				lRc = whReturnByBox();
				if (lRc != _NORMAL) {
					return lRc;
				}
				break;
			}
			case 'T': {// 已出货大片返回产线
				lRc = outReturnByPnl();
				if (lRc != _NORMAL) {
					return lRc;
				}
				break;
			}
			case 'L': {// 已出货大片返回产线
				lRc = outReturnByPnl();
				if (lRc != _NORMAL) {
					return lRc;
				}
				break;
			}
			case 'X': {
				lRc = whChgX();
				if (lRc != _NORMAL) {
					return lRc;
				}
				break;
			}
			case 'Y': {
				lRc = whChgY();
				if (lRc != _NORMAL) {
					return lRc;
				}
				break;
			}
			case 'D': {
				lRc = whOutbydn();
				if (lRc != _NORMAL) {
					return lRc;
				}
				break;
			}
			case 'A': {
				lRc = whOutbywo();
				if (lRc != _NORMAL) {
					return lRc;
				}
				break;
			}
			case 'E': {
				lRc = exportToFtpProc();
				if (lRc != _NORMAL) {
					return lRc;
				}
				break;
			}
			case 'F': {
				lRc = reExportToCsotFtpProc();
				if (lRc != _NORMAL) {
					return lRc;
				}
				break;
			}
			case 'P': {
				lRc = packBox();
				if (lRc != _NORMAL) {
					return lRc;
				}
				break;
			}
			case 'U': {
				lRc = unpackBox();
				if (lRc != _NORMAL) {
					return lRc;
				}
				break;
			}
			case 'Z': {
				lRc = reExportXmtmProc();
				if (lRc != _NORMAL) {
					return lRc;
				}
				break;
			}
			default: {
				gszMsgBuf = "Invalide type input: [" + actionType + "]";
				return E_XPWMSOPE_INVALID_INPUT;
			}
		}
		return lRc;
	}

	private long outReturnByPnl() {
		long lRc = _NORMAL;
		String nx_ope_no;
		String nx_ope_id = "";
		String nx_ope_ver = "";
		String nx_tool_Group = "";
		String nx_proc_id = "";
		String path_id = "", path_ver = "", ope_id = "";
		String box_id = "", wo_id = "", cus_id = "";
		String evt_usr = inTrx.getEvt_user();
		Integer prd_qty = 0;
		XpwmsopeIA iary = inTrx.getIary().get(0);
		box_id = iary.getBox_id();
		wo_id = iary.getWo_id_fk();
		cus_id = inTrx.getCus_id();
		/**
		 * Check Box Info
		 */
		Ret_prd_in ret_prd_in = new Ret_prd_in();
		Ret_prd_inId in_key = new Ret_prd_inId();
		in_key.setMtrl_box_id(box_id);
		in_key.setCus_id(cus_id);
		ret_prd_in = retPrdInDao.get(in_key);
		lRc = checkBoxInf(ret_prd_in, box_id, cus_id);
		if (lRc != _NORMAL) {
			return lRc;
		}
		/**
		 * Check Wo Info
		 */
		Ret_wo ret_wo = retWODao.get(wo_id);
		lRc = checkWoInf(ret_wo);
		if (lRc != _NORMAL) {
			return lRc;
		}
		path_id = ret_wo.getPath_id_fk();
		path_ver = ret_wo.getPath_ver_fk();
		ope_id = ret_wo.getStr_ope_no();
		/**
		 * Get Operation Info
		 */
		Bis_pathId nxPathId = new Bis_pathId();
		Bis_path nxPath = new Bis_path();

		Bis_opeId nx_opeID = new Bis_opeId();
		Bis_ope nx_ope = new Bis_ope();

		nxPathId.setPath_id_fk(path_id);
		nxPathId.setPath_ver_fk(path_ver);
		nxPathId.setCr_ope_no(ope_id);
		nxPath = bisPathDao.get(nxPathId);

		if (nxPath == null) {
			gszMsgBuf = "找不到此工艺路线对应的这个站点，请核查！";
			return E_BIS_PATH + E_READ_NOT_FOUND;
		}
		nx_opeID.setOpe_id(nxPath.getCr_ope_id_fk());
		nx_opeID.setOpe_ver(nxPath.getCr_ope_ver_fk());
		nx_ope = bisOpeDao.get(nx_opeID);
		if (nx_ope == null) {
			gszMsgBuf = "找不到这个站点，请核查！";
			return E_BIS_OPE + E_READ_NOT_FOUND;
		}
		/**
		 * 获取下一个站点的信息
		 */
		nx_ope_no = nxPath.getId().getCr_ope_no();
		nx_ope_id = nxPath.getCr_ope_id_fk();
		nx_ope_ver = nxPath.getCr_ope_ver_fk();
		nx_tool_Group = nx_ope.getToolg_id();
		nx_proc_id = nx_ope.getProc_id();
		/**
		 * Check Product Info
		 */
		if (inTrx.getIaryB().size() > 0) {
			for (XpwmsopeIB iaryB : inTrx.getIaryB()) {
				Ret_prd_info ret_prd_info = retPrdInfoDao.get(iaryB
						.getPrd_seq_id());
				if (inTrx.getAction_flg().equals("T")) {
					if (ret_prd_info == null) {
						if (iaryB.getPrd_qty() == null
								|| iaryB.getPrd_qty().equals(0)) {
							gszMsgBuf = "此片面板在系统中不存在，必须输入大板数量！";
							return E_MPWMSOPE_INVALID_QTY;
						}
						prd_qty += iaryB.getPrd_qty();
						Ret_prd_info ret_prd_info2 = new Ret_prd_info();
						/**
						 * Update Product Info(Ret_prd_info)
						 */
						ret_prd_info2.setPrd_seq_id(iaryB.getPrd_seq_id());
						ret_prd_info2.setUnq_seq_id(GUIDGenerator.javaGUID());
						ret_prd_info2.setEvt_seq_id(GUIDGenerator.javaGUID());
						updatePrdNxInfProc(ret_prd_info2, ret_wo, ret_prd_in,
								iaryB.getSlot_no(), box_id, path_id, path_ver,
								nx_ope_id, nx_ope_no, nx_ope_ver,
								nx_tool_Group, nx_proc_id, evt_usr,
								iaryB.getPrd_qty());
						retPrdInfoDao.save(ret_prd_info2);
					} else {
						if (!ret_prd_info.getPrd_stat().equals(PROC_SHTC)) {
							gszMsgBuf = "此片玻璃的状态不是出货状态，不可以做返工！";
							return E_MPWMSOPE_INVALID_STAT;
						}
						prd_qty += ret_prd_info.getPrd_qty();
						/**
						 * Update Product Info(Ret_prd_info)
						 */
						updatePrdNxInfProc(ret_prd_info, ret_wo, ret_prd_in,
								iaryB.getSlot_no(), box_id, path_id, path_ver,
								nx_ope_id, nx_ope_no, nx_ope_ver,
								nx_tool_Group, nx_proc_id, evt_usr,
								ret_prd_info.getPrd_qty());
						retPrdInfoDao.update(ret_prd_info);
					}

				} else if (inTrx.getAction_flg().equals("L")) {
					if (ret_prd_info != null) {
						gszMsgBuf = "此小粒批次已经存在于系统中,请核查！";
						return E_RET_PRD_INFO + E_ADD_EXIST;
					}
					prd_qty += iaryB.getPrd_qty();
					Ret_prd_info ret_prd_info2 = new Ret_prd_info();
					/**
					 * Update Product Info(Ret_prd_info)
					 */
					ret_prd_info2.setPrd_seq_id(iaryB.getPrd_seq_id());
					updatePrdNxInfProc(ret_prd_info2, ret_wo, ret_prd_in,
							iaryB.getSlot_no(), box_id, path_id, path_ver,
							nx_ope_id, nx_ope_no, nx_ope_ver, nx_tool_Group,
							nx_proc_id, evt_usr, iaryB.getPrd_qty());
					retPrdInfoDao.save(ret_prd_info2);
				}
			}
		}
		/**
		 * Check qty
		 */
		lRc = checkQtyInf(inTrx.getIaryB().size(), ret_prd_in, ret_wo);
		if (lRc != _NORMAL) {
			return lRc;
		}
		/**
		 * Update Ret_box Info
		 */
		Ret_box ret_box = retBoxDao.getWithLock(box_id, LockMode.UPGRADE);
		if (ret_box == null) {
			Ret_box ret_box2 = new Ret_box();
			updateBoxNxInfoProc(ret_box2, ret_wo, box_id, inTrx.getIaryB()
					.size(), path_id, path_ver, nx_ope_id, nx_ope_no,
					nx_ope_ver, nx_tool_Group, nx_proc_id, evt_usr);
			retBoxDao.save(ret_box2);
		} else {
			updateBoxNxInfoProc(ret_box, ret_wo, box_id, inTrx.getIaryB()
					.size(), path_id, path_ver, nx_ope_id, nx_ope_no,
					nx_ope_ver, nx_tool_Group, nx_proc_id, evt_usr);
			retBoxDao.update(ret_box);
		}

		/**
		 * Update Ret_prd_in Info
		 */
		ret_prd_in.setPpbox_stat(PROC_EMPTY);
		ret_prd_in.setPrd_act_qty(ret_prd_in.getPrd_act_qty()
				- inTrx.getIaryB().size());
		ret_prd_in.setPrd_proc_qty(ret_prd_in.getPrd_proc_qty()
				+ inTrx.getIaryB().size());
		ret_prd_in.setEvt_usr(evt_usr);
		ret_prd_in.setEvt_timestamp(gEvtTimestamp);
		retPrdInDao.update(ret_prd_in);
		/**
		 * Update Ret_wo Info
		 */
		ret_wo.setRl_prd_qty(ret_wo.getRl_prd_qty() + prd_qty);
		ret_wo.setEvt_usr(TX_NAME);
		ret_wo.setEvt_timestamp(gEvtTimestamp);
		retWODao.update(ret_wo);
		return lRc;
	}

	private long whReturnByBox() {
		long lRc = _NORMAL;
		Ret_wo ret_wo = new Ret_wo();
		Ret_wo new_ret_wo = new Ret_wo();
		String nx_ope_no;
		String nx_ope_id;
		String nx_ope_ver;
		String nx_tool_Group;
		String nx_proc_id;
		String path_id, path_ver;
		String final_wo_id;

		String ope_id = inTrx.getOpe_id();

		if (inTrx.getBox_cnt() > 0) {

			// Get old wo info
			ret_wo = retWODao.getWithLock(inTrx.getWo_id(), LockMode.UPGRADE);
			if (ret_wo == null) {
				gszMsgBuf = new NotFoundRtnBeen(inTrx.getWo_id(), Ret_wo.class)
						.toString();
				return E_RET_WO + E_READ_NOT_FOUND;
			}

			if (WO_STATUS_CLOS.equals(ret_wo.getWo_stat())) {
				gszMsgBuf = "内部订单[" + inTrx.getWo_id() + "]已经关闭，请先打开后再返工！";
				return E_XPAPLYWO_CLOSE_STAT;
			}
			final_wo_id = ret_wo.getWo_id();

			// Get new wo info
			if (_YES.equals(inTrx.getUse_new_wo_id())) {
				if(inTrx.getWo_id().equals(inTrx.getUse_new_wo_id())){
					gszMsgBuf = "新内部订单与原内部订单相同，请确认！";
					return E_XPWMSOPE_SAME_WO;					
				}
				new_ret_wo = retWODao.getWithLock(inTrx.getNew_wo_id(),
						LockMode.UPGRADE);
				if (new_ret_wo == null) {
					gszMsgBuf = new NotFoundRtnBeen(inTrx.getNew_wo_id(),
							Ret_wo.class).toString();
					return E_RET_WO + E_READ_NOT_FOUND;
				}

				if (!WO_STATUS_WAIT.equals(new_ret_wo.getWo_stat())) {
					gszMsgBuf = "新内部订单[" + inTrx.getNew_wo_id() + "]状态异常，请确认！";
					return E_XPAPLYWO_CLOSE_STAT;
				}
				Bis_mdl_def bismdl_o = bisMdlDefDao.get(ret_wo.getMdl_id_fk());
				Bis_mdl_def bismdl_n = bisMdlDefDao.get(new_ret_wo.getMdl_id_fk());
				if (!bismdl_o.getLayot_id_fk().equals(bismdl_n.getLayot_id_fk())) {
					gszMsgBuf = "两个工单对应的版式不同，不能转，请确认！";
					return E_XPWMSOPE_SAME_WO;	
				}
				if (!ret_wo.getCus_id_fk().equals(new_ret_wo.getCus_id_fk())){
					gszMsgBuf = "两个工单对应的客户不同，不能转，请确认！";
					return E_XPWMSOPE_SAME_WO;	
				}
				
				final_wo_id = new_ret_wo.getWo_id();
			}

			for (XpwmsopeIA iary : inTrx.getIary()) {
				Ret_box box = new Ret_box();
				List<Ret_prd_info> ret_prd_infoList = new ArrayList<Ret_prd_info>();

				// Get box
				String box_id = iary.getBox_id();
				box = retBoxDao.getWithLock(box_id, LockMode.UPGRADE);
				if (null == box) {
					gszMsgBuf = new NotFoundRtnBeen(box_id, Ret_box.class)
							.toString();
					return E_RET_BOX + E_READ_NOT_FOUND;
				}

				if (!PROC_SHIP.equals(box.getBox_stat())) {
					gszMsgBuf = "箱子[" + box.getBox_id() + "]不在仓库，请确认！";
					return INVALIED_BOX_STAT;
				}

				// Get prd info
				ret_prd_infoList = retPrdInfoDao.getShtListInfoWithLock(box_id);
				if (null == ret_prd_infoList) {
					gszMsgBuf = "箱子中没有玻璃，请核查！";
					return E_RET_PRD_INFO + E_READ_NOT_FOUND;
				}

				// Process prd info
				Bis_pathId nxPathId = new Bis_pathId();
				Bis_path nxPath = new Bis_path();

				Bis_opeId nx_opeID = new Bis_opeId();
				Bis_ope nx_ope = new Bis_ope();

				if (_YES.equals(inTrx.getUse_new_wo_id())) {
					// Use new wo
					nxPathId.setPath_id_fk(new_ret_wo.getPath_id_fk());
					nxPathId.setPath_ver_fk(new_ret_wo.getPath_ver_fk());
					nxPathId.setCr_ope_no(new_ret_wo.getStr_ope_no());
					nxPath = bisPathDao.get(nxPathId);
					if (nxPath == null) {
						gszMsgBuf = "找不到此工艺路线对应的这个站点，请核查！";
						return E_BIS_PATH + E_READ_NOT_FOUND;
					}

					nx_opeID.setOpe_id(nxPath.getCr_ope_id_fk());
					nx_opeID.setOpe_ver(nxPath.getCr_ope_ver_fk());
					nx_ope = bisOpeDao.get(nx_opeID);
					if (nx_ope == null) {
						gszMsgBuf = "找不到这个站点，请核查！";
						return E_BIS_OPE + E_READ_NOT_FOUND;
					}

					// change wo
					if (box.getBnk_flg().equals("1")){
						ret_wo.setWh_in_prd_qty(ret_wo.getWh_in_prd_qty()
								- box.getPrd_qty());
					}else if(box.getBnk_flg().equals("2")){
						ret_wo.setWip_wh_in_prd_qty(ret_wo.getWip_wh_in_prd_qty()- box.getPrd_qty()) ;
					}
					ret_wo.setPln_prd_qty(ret_wo.getPln_prd_qty()
							- box.getPrd_qty());
					ret_wo.setRl_prd_qty(ret_wo.getRl_prd_qty()
							- box.getPrd_qty());
					//set rl_cnt
					ret_wo.setEvt_timestamp(gEvtTimestamp);
					ret_wo.setEvt_usr(inTrx.getEvt_user());
					retWODao.update(ret_wo);
					new_ret_wo.setPln_prd_qty(new_ret_wo.getPln_prd_qty()
							+ box.getPrd_qty());
					new_ret_wo.setRl_prd_qty(new_ret_wo.getRl_prd_qty()
							+ box.getPrd_qty());
					if (new_ret_wo.getPln_prd_qty() > new_ret_wo
							.getRsv_pln_prd_qty()) {
						gszMsgBuf = "新内部订单[" + inTrx.getNew_wo_id()
								+ "]来料数量大于计划数量，请确认！";
						return E_XPWMSOPE_WO_CNT_NOT_ENOUGH;
					}
					if (new_ret_wo.getRl_prd_qty() > new_ret_wo
							.getPln_prd_qty()) {
						gszMsgBuf = "新内部订单[" + inTrx.getNew_wo_id()
								+ "]下线数量大于来料数量，请确认！";
						return E_XPWMSOPE_WO_CNT_NOT_ENOUGH;
					}
					if(null == new_ret_wo.getAct_stb_timestamp()&& null != ret_wo.getAct_stb_timestamp()){
						new_ret_wo.setAct_stb_timestamp(ret_wo.getAct_stb_timestamp());
					}
					//set rl_cnt
					new_ret_wo.setEvt_timestamp(gEvtTimestamp);
					new_ret_wo.setEvt_usr(inTrx.getEvt_user());
					retWODao.update(new_ret_wo);

				} else {
					// Use old wo
					Ret_prd_info ret_prd_info = ret_prd_infoList.get(0);

					//NO.01  Fix 返工工艺路线改变问题
					if(ret_prd_info.getNx_path_id_fk()==null || "".equals(ret_prd_info.getNx_path_id_fk().trim())){
						nxPathId.setPath_id_fk(ret_prd_info.getCr_path_id_fk());
					}else{
						nxPathId.setPath_id_fk(ret_prd_info.getNx_path_id_fk());
					}
					if(ret_prd_info.getNx_path_ver_fk()==null || "".equals(ret_prd_info.getNx_path_ver_fk().trim())){
						nxPathId.setPath_ver_fk(ret_prd_info.getCr_path_ver_fk());
					}else{
						nxPathId.setPath_ver_fk(ret_prd_info.getNx_path_ver_fk());
					}

					nxPathId.setCr_ope_no(ope_id);
					nxPath = bisPathDao.get(nxPathId);
					if (null == nxPath) {
						
						nxPathId.setCr_ope_no(ope_id.replace("-", ""));
						nxPath = bisPathDao.get(nxPathId);
						if (null == nxPath) {
						     gszMsgBuf = "找不到此工艺路线对应的这个站点，请核查！";
						     return E_BIS_PATH + E_READ_NOT_FOUND;
						}
					}

					nx_opeID.setOpe_id(nxPath.getCr_ope_id_fk());
					nx_opeID.setOpe_ver(nxPath.getCr_ope_ver_fk());
					nx_ope = bisOpeDao.get(nx_opeID);
					if (nx_ope == null) {
						gszMsgBuf = "找不到这个站点，请核查！";
						return E_BIS_OPE + E_READ_NOT_FOUND;
					}

					// Update Ret_wo qty of WH_IN
					if (box.getBnk_flg().equals("1")){
						ret_wo.setWh_in_prd_qty(ret_wo.getWh_in_prd_qty()
								- box.getPrd_qty());
					}else if(box.getBnk_flg().equals("2")){
						ret_wo.setWip_wh_in_prd_qty(ret_wo.getWip_wh_in_prd_qty()- box.getPrd_qty()) ;
					}
					ret_wo.setEvt_timestamp(gEvtTimestamp);
					ret_wo.setEvt_usr(inTrx.getEvt_user());
					retWODao.update(ret_wo);
				}

				// 获取下一个站点的信息
				path_id = nxPathId.getPath_id_fk();
				path_ver = nxPathId.getPath_ver_fk();
				nx_ope_no = nxPath.getId().getCr_ope_no();
				nx_ope_id = nxPath.getCr_ope_id_fk();
				nx_ope_ver = nxPath.getCr_ope_ver_fk();
				nx_tool_Group = nx_ope.getToolg_id();
				nx_proc_id = nx_ope.getProc_id();

				// Update Prd Next Operation Info
				for (Ret_prd_info prdInfo : ret_prd_infoList) {
					setPrdNxInf(prdInfo, nx_ope_id, nx_ope_no, nx_ope_ver,
							path_id, path_ver, nx_proc_id, nx_tool_Group,
							final_wo_id);
					retPrdInfoDao.update(prdInfo);
				}

				// Update Box Next Operation Info
				setBoxNxInf(box, nx_ope_id, nx_ope_no, nx_ope_ver, path_id,
						path_ver, nx_proc_id, nx_tool_Group, final_wo_id);
				retBoxDao.update(box);
			} // End of for
		}
		return lRc;
	}

	private long getWhsInfo() {
		List<Ret_box> ret_boxList = new ArrayList<Ret_box>();

		String hqlQuery = "FROM Ret_box where 1=1 ";
		if (!Util.isSpaceCheck(inTrx.getDest_shop())) {
			hqlQuery += " AND dest_shop = '" + inTrx.getDest_shop() + "' ";
		}
		if (!Util.isSpaceCheck(inTrx.getBox_stat())) {
			hqlQuery += " AND box_stat = '" + inTrx.getBox_stat() + "' ";
		}

		ret_boxList = retBoxDao.find(hqlQuery);
		outTrx.setBox_cnt(0);
		if (!ret_boxList.isEmpty()) {
			outTrx.setBox_cnt(ret_boxList.size());
			outTrx.setOary(ret_boxList);
		}

		return _NORMAL;
	}

	private long getWhsInfo2() {
		List<Ret_box> ret_boxList = new ArrayList<Ret_box>();

		String hqlQuery = "FROM Ret_box where 1=1 ";
		if (!Util.isSpaceCheck(inTrx.getDest_shop())) {
			hqlQuery += " AND dest_shop = '" + inTrx.getDest_shop() + "' ";
		}
		if (!Util.isSpaceCheck(inTrx.getBox_stat())) {
			hqlQuery += " AND box_stat = '" + inTrx.getBox_stat() + "' ";
		}

		ret_boxList = retBoxDao.find(hqlQuery);
		outTrx.setBox_cnt(0);
		if (!ret_boxList.isEmpty()) {
			List<XpwmsopeOA> oary2 = new ArrayList<XpwmsopeOA>();
			for (Ret_box box : ret_boxList) {
				XpwmsopeOA xpwmsopeOA = new XpwmsopeOA();
				xpwmsopeOA.setBox_id(box.getBox_id());
				xpwmsopeOA.setBatch_no_fk(box.getBatch_no_fk());
				xpwmsopeOA.setNg_grade_cnt(retPrdInfoDao.getNgPrdCnt(box
						.getBox_id()));
				xpwmsopeOA.setPrd_qty(box.getPrd_qty());
				xpwmsopeOA.setBox_weight(box.getBox_weight());
				xpwmsopeOA.setMdl_id_fk(box.getMdl_id_fk());
				xpwmsopeOA.setDest_shop(box.getDest_shop());
				xpwmsopeOA.setWo_id_fk(box.getWo_id_fk());
				xpwmsopeOA.setSo_id_fk(box.getSo_id_fk());
				xpwmsopeOA.setBox_stat(box.getBox_stat());

				oary2.add(xpwmsopeOA);
			}
			outTrx.setBox_cnt(ret_boxList.size());
			outTrx.setOary2(oary2);
		}
		return _NORMAL;
	}

	/**
	 * WIP dest_shop box back
	 * 
	 * @return
	 */
	private long wipBack() {
		if (inTrx.getBox_cnt() > 0) {
			for (XpwmsopeIA iary : inTrx.getIary()) {
				Ret_wo ret_wo = new Ret_wo();
				ret_wo = retWODao.get(iary.getWo_id_fk());
				if (null == ret_wo) {
					gszMsgBuf = new NotFoundRtnBeen(iary.getWo_id_fk(),
							Ret_wo.class).toString();
					return E_RET_WO + E_READ_NOT_FOUND;
				}

				// Get box
				Ret_box box = new Ret_box();
				box = retBoxDao.getWithLock(iary.getBox_id(), LockMode.UPGRADE);
				if (null == box) {
					gszMsgBuf = "在["
							+ Ret_box.class.getSimpleName().toUpperCase()
							+ "]中找不到箱号 [" + iary.getBox_id() + "]的信息！";
					return E_RET_BOX + E_READ_NOT_FOUND;
				}

				if (!ret_wo.getWo_id().equals(box.getWo_id_fk())) {
					gszMsgBuf = "箱子 [" + iary.getBox_id() + "] 订单号为["
							+ box.getWo_id_fk() + "]，不属于订单["
							+ ret_wo.getWo_id() + "]";
					return E_XPWMSOPE_WO_UNMATCH;
				}

				// Check box info
				if (!PROC_SHIP.equals(box.getBox_stat())) {
					gszMsgBuf = "箱子[" + box.getBox_id() + "] 状态是 ["
							+ box.getBox_stat() + "],必须是SHIP";
					return INVALIED_BOX_STAT;
				}

				if (null != box.getPrd_admin_flg()
						&& box.getPrd_admin_flg().substring(0, 1).equals("Y")) {
					gszMsgBuf = "箱子[" + box.getBox_id()
							+ "] 出货客户状态是 [ Y ],必须是N，方可返回产线！";
					return E_XPWMSOPE_ADMIN_FLG;
				}

				// Fetch panels
				List<Ret_prd_info> prdInfoList = new ArrayList<Ret_prd_info>();
				prdInfoList = retPrdInfoDao.getShtListInfo(iary.getBox_id());
				if (null != prdInfoList) {
					if (Long.valueOf(iary.getPrd_qty()) == prdInfoList.size()
							&& prdInfoList.size() == box.getPrd_qty()) {
						boolean isChangeProc = retPrdInfoDao.isChangeProcFlg(prdInfoList.get(0));
							
						for (Ret_prd_info item : prdInfoList) {
							if (!PROC_SHIP.equals(item.getPrd_stat())) {
								gszMsgBuf = "玻璃状态必须是 " + PROC_SHIP + ", ["
										+ item.getPrd_seq_id() + "] 状态是 ["
										+ item.getPrd_stat() + "]";
								return INVALIED_SHT_STAT;
							}
							if(isChangeProc){
								item.setQrs_ovr_flg("Y");
							}
							item.setPv_prd_stat_fk(item.getPrd_stat());
							item.setPrd_stat(PROC_WAIT);
							item.setEvt_cate(EVT_WIP_WHIO);
							item.setRelate_usr(inTrx.getRelate_usr());
							item.setBnk_flg(BANK_OUT);
							item.setEvt_usr(inTrx.getEvt_user());
							item.setEvt_timestamp(gEvtTimestamp);
							item.setEvt_time_d(gEvtTimeD);
							item.setWh_in_timestamp(gEvtTimestamp);
							retPrdInfoDao.update(item);
						}
					} else {
						gszMsgBuf = "箱子 [" + iary.getBox_id() + "]中产品数量为 ["
								+ iary.getPrd_qty() + "] 但是RET_PRD_INFO只有 ["
								+ prdInfoList.size() + "].";
						return INVALIED_SHT_CNT;
					}
				} else {
					gszMsgBuf = "在[" + iary.getBox_id() + "]中未找到产品信息";
					return E_XPWMSOPE_FIND_NO_PANEL_BY_BOX;
				}
				// Set box
				box.setBox_stat(PROC_WAIT);
				box.setBnk_flg(BANK_OUT);
				box.setDest_shop(_SPACE);
				box.setEvt_cate(EVT_WIP_WHIO);
				box.setEvt_usr(inTrx.getEvt_user());
				box.setEvt_timestamp(gEvtTimestamp);
				box.setEvt_time_d(gEvtTimeD);
				retBoxDao.update(box);
				
				ret_wo.setEvt_timestamp(gEvtTimestamp);
				ret_wo.setEvt_usr(inTrx.getEvt_user());
				ret_wo.setWip_wh_in_prd_qty(ret_wo.getWip_wh_in_prd_qty()
						- box.getPrd_qty());
				ret_wo.setWip_wh_out_prd_qty(ret_wo.getWip_wh_out_prd_qty()
						+ box.getPrd_qty());
				retWODao.update(ret_wo);
			}
		}
		return _NORMAL;
	}

	/**
	 * whouse in operation
	 * 
	 * @return
	 */
	private long whIn() {
		SimpleRtnBeen rtn = new SimpleRtnBeen();
		long lRc = _NORMAL;
		HashMap<String, Integer> dpsCntMap = new HashMap<String, Integer>();
		// long lWhInCnt = 0;
		boolean isWipDestShop = false;
		boolean isShipCus = false;
		String shopFAB = "";
		
		String sql = "FROM Bis_data where data_cate = 'DEST' and data_id = '" + inTrx.getDest_shop() + "'" ;
		List<Bis_data> shopList = new ArrayList<Bis_data>();
		shopList = bisDataDao.find(sql);
		if(shopList.isEmpty()){
			gszMsgBuf = "仓别[" + inTrx.getDest_shop() + "]不存在！";
			return E_READ_NOT_FOUND;
		}else {
			shopFAB = shopList.get(0).getData_item();
		}
		// Check wip destshop shpping logic
		if (!Util.isSpaceCheck(inTrx.getWip_bank_flg())) {
			if (_YES.equals(inTrx.getWip_bank_flg())) {
				isWipDestShop = true;
			} else if (_NO.equals(inTrx.getWip_bank_flg())) {
				isWipDestShop = false;
			} else {
				gszMsgBuf = "Wip dest shop shipping flg ["
						+ inTrx.getWip_bank_flg() + "] is invalied!";
				return E_XPWMSOPE_IN_WIP_BANK_SHIPPING_FLG_INVALIED;
			}
		}

		if (!Util.isSpaceCheck(inTrx.getIsShipCus())) {
			if (inTrx.getIsShipCus().equals("true")) {
				isShipCus = true;
			} else {
				isShipCus = false;
			}
		}

		if (inTrx.getBox_cnt() > 0) {
			for (XpwmsopeIA iary : inTrx.getIary()) {
				Ret_wo ret_wo = new Ret_wo();
				ret_wo = retWODao.getWithLock(iary.getWo_id_fk(),
						LockMode.UPGRADE);
				if (null == ret_wo) {
					gszMsgBuf = new NotFoundRtnBeen(iary.getWo_id_fk(),
							Ret_wo.class).toString();
					return E_RET_WO + E_READ_NOT_FOUND;
				}

				// Get box
				Ret_box box = new Ret_box();
				box = retBoxDao.getWithLock(iary.getBox_id(), LockMode.UPGRADE);
				if (null == box) {
					gszMsgBuf = " Not found box [" + iary.getBox_id()
							+ "] in ["
							+ Ret_box.class.getSimpleName().toUpperCase()
							+ "]!";
					return E_RET_BOX + E_READ_NOT_FOUND;
				}

				if (!ret_wo.getWo_id().equals(box.getWo_id_fk())) {
					gszMsgBuf = "箱子 [" + iary.getBox_id() + "] 订单号为["
							+ box.getWo_id_fk() + "]，不属于订单["
							+ ret_wo.getWo_id() + "]";
					return E_XPWMSOPE_WO_UNMATCH;
				}

				// Check box info
				if (isWipDestShop) {
					if (!PROC_WAIT.equals(box.getBox_stat())) {
						gszMsgBuf = "半成品入库的箱子状态必须为" + PROC_WAIT + "， ["
								+ box.getBox_id() + "] 当前状态是 ["
								+ box.getBox_stat() + "]";
						return INVALIED_BOX_STAT;
					}
				} else if (!PROC_COMPLETE.equals(box.getBox_stat())) {
					gszMsgBuf = "入库的箱子状态必须是 " + PROC_COMPLETE + ", ["
							+ box.getBox_id() + "] 当前状态是 [" + box.getBox_stat()
							+ "]";
					return INVALIED_BOX_STAT;
				}

				// Fetch panels
				List<Ret_prd_info> prdInfoList = new ArrayList<Ret_prd_info>();
				prdInfoList = retPrdInfoDao.getShtListInfo(iary.getBox_id());
				if (null != prdInfoList) {
					if (Long.valueOf(iary.getPrd_qty()) == prdInfoList.size() && prdInfoList.size() == box.getPrd_qty()) {
						if (isWipDestShop) {
							if (!PROC_WAIT.equals(prdInfoList.get(0).getPrd_stat())) {
								gszMsgBuf = "半成品入库的玻璃状态必须为 " + PROC_WAIT
										+ "， [" + prdInfoList.get(0).getPrd_seq_id()
										+ "] 当前状态是 [" + prdInfoList.get(0).getPrd_stat()
										+ "]";
								return INVALIED_SHT_STAT;
							}
						} else {
							if (!PROC_COMPLETE.equals(prdInfoList.get(0).getPrd_stat())) {
								gszMsgBuf = "半成品入库的玻璃状态必须为 " + PROC_COMPLETE
										+ ", [" + prdInfoList.get(0).getPrd_seq_id()
										+ "] 当前状态是 [" + prdInfoList.get(0).getPrd_stat()
										+ "]";
								return INVALIED_SHT_STAT;
							}
						
						}
						for (Ret_prd_info item : prdInfoList) {
							if(_YES.equals(ret_wo.getSwh_cnt_flg())){//客户来料工单
								if(!prdInfoList.get(0).getGroup_id().equals(item.getGroup_id())){
									gszMsgBuf = "箱号[" + iary.getBox_id() + "]玻璃的组代码不能混！[" + prdInfoList.get(0).getPrd_seq_id()+ "]["
										+ prdInfoList.get(0).getGroup_id() + "] vs ["
										+ item.getPrd_seq_id() + "]["
										+ item.getGroup_id() + "]";
									return GROUP_ID_MIX;
								}
								if(!prdInfoList.get(0).getPrd_grade().equals(item.getPrd_grade())){
									gszMsgBuf = "箱号[" + iary.getBox_id() + "]玻璃的等级不能混！[" + prdInfoList.get(0).getPrd_seq_id()+ "]["
										+ prdInfoList.get(0).getPrd_grade() + "] vs ["
										+ item.getPrd_seq_id() + "]["
										+ item.getPrd_grade() + "]";
									return PRD_GRADE_MIX;
								}
							}
							item.setPv_prd_stat_fk(item.getPrd_stat());
							item.setPrd_stat(PROC_SHIP);
							if (isWipDestShop) {
								item.setEvt_cate(EVT_WIP_WHIN);
								item.setBnk_flg(DEST_SHOP_BANK_FLG_WIP);
							} else {
								item.setEvt_cate(EVT_WHIN);
								item.setBnk_flg(DEST_SHOP_BANK_FLG_NO_WIP);
							}
							item.setDest_shop(iary.getDest_shop());
							item.setEvt_usr(inTrx.getEvt_user());
							item.setRelate_usr(inTrx.getRelate_usr());
							item.setEvt_timestamp(gEvtTimestamp);
							item.setEvt_time_d(gEvtTimeD);
							item.setWh_in_timestamp(gEvtTimestamp);
							if (isShipCus) {
								if (null != item.getPrd_admin_flg()) {
									StringBuffer prd_admin_flg = new StringBuffer(
											item.getPrd_admin_flg());
									prd_admin_flg.setCharAt(0, 'Y');
									item.setPrd_admin_flg(prd_admin_flg
											.toString());
								} else {
									item.setPrd_admin_flg("YNNNNNNNNNNNNNNNNNNN");
								}
							}
							retPrdInfoDao.update(item);
						}
					} else {
						gszMsgBuf = "Box [" + iary.getBox_id() + "] has ["
								+ iary.getPrd_qty()
								+ "] panels, but only find ["
								+ prdInfoList.size() + "].";
						return INVALIED_SHT_CNT;
					}
				} else {
					gszMsgBuf = "No panel in box [" + iary.getBox_id() + "].";
					return E_XPWMSOPE_FIND_NO_PANEL_BY_BOX;
				}

				box.setBox_stat(PROC_SHIP);
				box.setWh_in_timestamp(gEvtTimestamp);
				box.setBox_weight(iary.getBox_weight());
				box.setEvt_usr(inTrx.getEvt_user());
				box.setRelate_usr(inTrx.getRelate_usr());
				box.setEvt_timestamp(gEvtTimestamp);
				box.setEvt_time_d(gEvtTimeD);
				box.setDest_shop(iary.getDest_shop());
				if (isWipDestShop) {
					box.setEvt_cate(EVT_WIP_WHIN);
					box.setBnk_flg(DEST_SHOP_BANK_FLG_WIP);
					box = retBoxDao.setBoxYield(box, prdInfoList);
				} else {
					box.setEvt_cate(EVT_WHIN);
					box.setBnk_flg(DEST_SHOP_BANK_FLG_NO_WIP);
				}

				if (isShipCus) {
					if (null != box.getPrd_admin_flg()) {
						StringBuffer prd_admin_flg = new StringBuffer(
								box.getPrd_admin_flg());
						prd_admin_flg.setCharAt(0, 'Y');
						box.setPrd_admin_flg(prd_admin_flg.toString());
					} else {
						box.setPrd_admin_flg("YNNNNNNNNNNNNNNNNNNN");
					}
				}
				retBoxDao.update(box);

				ret_wo.setEvt_timestamp(gEvtTimestamp);
				ret_wo.setEvt_usr(inTrx.getEvt_user());
				if (!isWipDestShop) {
					ret_wo.setWh_in_prd_qty(ret_wo.getWh_in_prd_qty()
							+ box.getPrd_qty());
					if (ret_wo.getWh_in_prd_qty().intValue()
							+ ret_wo.getScrp_prd_qty().intValue() > ret_wo
							.getRl_prd_qty().intValue()) {
						gszMsgBuf = "内部订单 [" + iary.getWo_id_fk() + "] 释放数量 ["
								+ ret_wo.getRl_prd_qty() + "],报废数量["
								+ ret_wo.getScrp_prd_qty() + "] + 入库数量 ["
								+ ret_wo.getWh_in_prd_qty() + "] 汇总超标！!";
						return E_XPWMSOPE_WO_CNT_NOT_ENOUGH;
					}
					ret_wo.setComp_prd_qty(ret_wo.getComp_prd_qty()
							- box.getPrd_qty());
					if (ret_wo.getComp_prd_qty().intValue() < 0) {
						gszMsgBuf = "内部订单 [" + iary.getWo_id_fk() + "] 完成数量 ["
								+ ret_wo.getComp_prd_qty() + "] 小于箱子数量 ["
								+ box.getPrd_qty() + "] !";
						return E_XPWMSOPE_WO_CNT_NOT_ENOUGH;
					}
				} else {
					ret_wo.setWip_wh_in_prd_qty(ret_wo.getWip_wh_in_prd_qty()
							+ box.getPrd_qty());
					if (ret_wo.getWip_wh_in_prd_qty().intValue() > ret_wo
							.getRl_prd_qty().intValue()) {
						gszMsgBuf = "内部订单 [" + iary.getWo_id_fk() + "] 释放数量 ["
								+ ret_wo.getRl_prd_qty() + "], 半成品入库数量 ["
								+ ret_wo.getWh_in_prd_qty() + "] 已经超标！";
						return E_XPWMSOPE_WO_CNT_NOT_ENOUGH;
					}
				}

				String curMdlCate = "";
				rtn = getCurMdl(box, ret_wo, isWipDestShop);
				if (rtn.getlRc() != _NORMAL) {
					return rtn.getlRc();
				}
				curMdlCate = (String) rtn.getObj();
				Ret_prd_info ret_prd_info = new Ret_prd_info();
				ret_prd_info = (Ret_prd_info) prdInfoList.get(0).clone();
				rtn = syncDpsInfoForWHInMOS(ret_wo.getWo_id(), box.getPrd_qty(), box,ret_prd_info, isWipDestShop);
				if (rtn.getlRc() != _NORMAL) {
					return rtn.getlRc();
				}
				dpsCntMap = (HashMap<String, Integer>) rtn.getObj();
				if (null != dpsCntMap) {
					Iterator iter = dpsCntMap.entrySet().iterator();
					int fetchPrdIndex = 0, prdCnt = prdInfoList.size();
					while (iter.hasNext()) {
						Map.Entry entry = (Map.Entry) iter.next();
						int val = (Integer) entry.getValue();
						String dpsId = (String) entry.getKey();
						for (int costCnt = 0; fetchPrdIndex <= prdCnt
								&& costCnt < val; costCnt++, fetchPrdIndex++) {
							if (MDL_CATE_JB.equals(curMdlCate)) {
								prdInfoList.get(fetchPrdIndex)
										.setFst_dps_id_fk(dpsId);
							} else if (MDL_CATE_DM.equals(curMdlCate)) {
								prdInfoList.get(fetchPrdIndex)
										.setSnd_dps_id_fk(dpsId);
							} else if (MDL_CATE_QG.equals(curMdlCate)) {
								prdInfoList.get(fetchPrdIndex)
										.setTrd_dps_id_fk(dpsId);
							}
						}
					}
				}

				rtn = retWODao.colseWo(ret_wo);
				if (rtn.getlRc() != _NORMAL) {
					return rtn.getlRc();
				}
				//统计等级率（LZ和NG）,内部订单全部产品完成成品入库之后（成品入库数+出货数+半成品出货（出货给客户）+ 报废数=投入数）
//				if((ret_wo.getWh_out_prd_qty().intValue()+ret_wo.getWh_in_prd_qty().intValue()
//						+ret_wo.getWip_wh_in_prd_qty().intValue()+ret_wo.getScrp_prd_qty().intValue()) == ret_wo.getPln_prd_qty()){
//					sendGradeRatioFunc(ret_wo.getCus_id_fk(),ret_wo.getWo_id(),
//							ret_wo.getSo_id(),ret_wo.getAct_stb_timestamp().toString().substring(0, 9),
//							ret_wo.getPln_prd_qty());
//				}
			}
		}
		return lRc;
	}

	/**
	 * whouse change destshop operation
	 * 
	 * @return
	 */
	private long whChg() {
		long lRc = _NORMAL;

		if (inTrx.getBox_cnt() > 0) {
			for (XpwmsopeIA iary : inTrx.getIary()) {

				// Get box
				Ret_box box = new Ret_box();
				box = retBoxDao.getWithLock(iary.getBox_id(), LockMode.UPGRADE);
				if (null == box) {
					gszMsgBuf = "箱子 [" + iary.getBox_id() + "] 不存在 ["
							+ Ret_box.class.getSimpleName().toUpperCase()
							+ "]!";
					return E_RET_BOX + E_READ_NOT_FOUND;
				}

				// Check box info
				if (!PROC_SHIP.equals(box.getBox_stat())) {
					gszMsgBuf = "转仓时箱子状态必须是 SHIP, [" + box.getBox_id()
							+ "] 当前状态是 [" + box.getBox_stat() + "]";
					return INVALIED_BOX_STAT;
				}
				if (inTrx.getDest_shop().equals(box.getDest_shop())) {
					gszMsgBuf = "仓别相同，不可转仓！";
					return E_XPWMSOPE_SAME_DEST_SHOP;
				}

				// Fetch panels
				List<Ret_prd_info> prdInfoList = new ArrayList<Ret_prd_info>();
				prdInfoList = retPrdInfoDao.getShtListInfo(iary.getBox_id());
				if (null != prdInfoList) {
					if (box.getPrd_qty() == prdInfoList.size()) {
						for (Ret_prd_info item : prdInfoList) {
							if (!PROC_SHIP.equals(item.getPrd_stat())) {
								gszMsgBuf = "转仓时玻璃状态必须是 SHIP, ["
										+ item.getPrd_seq_id() + "] 当前状态是 ["
										+ item.getPrd_stat() + "]";
								return INVALIED_SHT_STAT;
							}

							item.setEvt_cate(EVT_WHCG);
							item.setDest_shop(inTrx.getDest_shop());
							item.setEvt_usr(inTrx.getEvt_user());
							item.setEvt_timestamp(gEvtTimestamp);
							item.setEvt_time_d(gEvtTimeD);
							retPrdInfoDao.update(item);
						}
					} else {
						gszMsgBuf = "Box [" + iary.getBox_id() + "] has ["
								+ iary.getPrd_qty()
								+ "] panels, but only find ["
								+ prdInfoList.size() + "].";
						return INVALIED_SHT_CNT;
					}
				} else {
					gszMsgBuf = "No panel in box [" + iary.getBox_id() + "].";
					return E_XPWMSOPE_FIND_NO_PANEL_BY_BOX;
				}

				box.setDest_shop(inTrx.getDest_shop());
				box.setEvt_cate(EVT_WHCG);
				box.setEvt_usr(inTrx.getEvt_user());
				box.setEvt_timestamp(gEvtTimestamp);
				box.setEvt_time_d(gEvtTimeD);

				retBoxDao.update(box);
			}
		}
		return lRc;
	}

	/**
	 * whouse shipping out operation
	 * 
	 * @return
	 * @throws ParseException 
	 */
	private long whOut() throws ParseException {
		long lRc = _NORMAL;
		Integer prdTotalCnt;
		SimpleRtnBeen rtnBeen = new SimpleRtnBeen(_NORMAL);
		Ret_box box = null ;
		Ret_wo ret_wo = null;
		String wo_id;
		List<Ret_prd_info> prdInfoList;
		if (inTrx.getBox_cnt() > 0) {
			prdTotalCnt = 0;
			wo_id="";
			for (XpwmsopeIA iary : inTrx.getIary()) {
				// Get box
				box = new Ret_box();
				box = retBoxDao.getWithLock(iary.getBox_id(), LockMode.UPGRADE);
				if (null == box) {
					gszMsgBuf = "箱子[" + iary.getBox_id() + "] 不存在 ["
							+ Ret_box.class.getSimpleName().toUpperCase()
							+ "]!";
					return E_RET_BOX + E_READ_NOT_FOUND;
				}

				// Check box info
				if (!PROC_SHIP.equals(box.getBox_stat())) {
					gszMsgBuf = "出货箱子状态必须是 SHIP, [" + box.getBox_id() + "] 是 ["
							+ box.getBox_stat() + "]";
					return INVALIED_BOX_STAT;
				}

				// WO wh_out_cnt check
				ret_wo = new Ret_wo();
				ret_wo = retWODao.getWithLock(box.getWo_id_fk(),
						LockMode.UPGRADE);
				if (null == ret_wo) {
					gszMsgBuf = new NotFoundRtnBeen(box.getWo_id_fk(),
							Ret_wo.class).toString();
					return E_RET_WO + E_READ_NOT_FOUND;
				}
				ret_wo.setWh_out_prd_qty(ret_wo.getWh_out_prd_qty()
						+ box.getPrd_qty());
				// Update for wip in
				if (retBoxDao.isInWipDestShop(box)) {
					ret_wo.setWip_wh_in_prd_qty(ret_wo.getWip_wh_in_prd_qty()
							- box.getPrd_qty());
					ret_wo.setWip_wh_out_prd_qty(ret_wo.getWip_wh_out_prd_qty()
							+ box.getPrd_qty());

				} else {// 仅当BOX为成品时才扣减此数量
					ret_wo.setWh_in_prd_qty(ret_wo.getWh_in_prd_qty()
							- box.getPrd_qty());
				}

				ret_wo.setEvt_timestamp(gEvtTimestamp);
				ret_wo.setEvt_usr(inTrx.getEvt_user());
				wo_id =  ret_wo.getWo_id();
				retWODao.colseWo(ret_wo);

				// Fetch panels
				prdInfoList = new ArrayList<Ret_prd_info>();
				String hql = "FROM Ret_prd_info where box_id_fk = ?";
				prdInfoList = retPrdInfoDao.listWithLock(hql, iary.getBox_id());
				if (!prdInfoList.isEmpty() && null != prdInfoList) {
					if (box.getPrd_qty() == prdInfoList.size()) {
						for (Ret_prd_info item : prdInfoList) {
							if (!PROC_SHIP.equals(item.getPrd_stat())) {
								gszMsgBuf = "出货的玻璃状态必须是 SHIP, ["
										+ item.getPrd_seq_id() + "] 当前状态是 ["
										+ item.getPrd_stat() + "]";
								return INVALIED_SHT_STAT;
							}

							item.setEvt_cate(EVT_WHOUT);
							item.setPrd_stat(PROC_SHTC);
							item.setEvt_usr(inTrx.getEvt_user());
							item.setEvt_timestamp(gEvtTimestamp);
							item.setEvt_time_d(gEvtTimeD);
							item.setShipping_timestamp(gEvtTimestamp);
							item.setShip_box_id_fk(item.getBox_id_fk());
							if (!Util.isSpaceCheck(inTrx.getPallet_id())) {
								item.setPallet_id(inTrx.getPallet_id());
							}
							item.setDn_no(inTrx.getDn_no());
							item.setSht_ope_msg(inTrx.getRemark());
							item.setPallet_id(item.getPallet_id());
							retPrdInfoDao.update(item);
						}

						String prd_gradeNG = prdInfoList.get(0).getPrd_grade();
						// 原箱check
						// 非OK品不做此check  -- 2015.01.26
//						if (_OK.equals(prd_gradeNG)) {
//							if (_YES.equals(ret_wo.getOpe_eff_flg())) {
//								rtnBeen = retBoxDao.chkMtrlBoxAndSlot(box, true);
//								if (_NORMAL != rtnBeen.getlRc()) {
//									gszMsgBuf = rtnBeen.getMsgBuf();
//									return rtnBeen.getlRc();
//								}
//							}
//						}
						
					} else {
						gszMsgBuf = "Box [" + iary.getBox_id() + "] has ["
								+ iary.getPrd_qty()
								+ "] panels, but only find ["
								+ prdInfoList.size() + "].";
						return INVALIED_SHT_CNT;
					}
				} else {
					gszMsgBuf = "No panel in box [" + iary.getBox_id() + "].";
					return E_XPWMSOPE_FIND_NO_PANEL_BY_BOX;
				}

				box.setBox_stat(PROC_SHTC);
				box.setShipping_timestamp(gEvtTimestamp);
				box.setRelate_usr(inTrx.getRelate_usr());
				if (!Util.isSpaceCheck(inTrx.getPallet_id())) {
					box.setPallet_id_fk(inTrx.getPallet_id());
				}
				box.setEvt_cate(EVT_WHOUT);
				box.setEvt_usr(inTrx.getEvt_user());
				box.setEvt_timestamp(gEvtTimestamp);
				box.setEvt_time_d(gEvtTimeD);
				retBoxDao.update(box);
				prdTotalCnt = prdTotalCnt + box.getPrd_qty();

				/**
				 * add RET_PRD_INFO_SHTC & RET_BOX_SHTC for RMA
				 */
				rtnBeen = addTableForSHTC(box, prdInfoList,ret_wo);
				if (_NORMAL != rtnBeen.getlRc()) {
					gszMsgBuf = rtnBeen.getMsgBuf();
					return rtnBeen.getlRc();
				}
			}
			/**
			 * 需要有返回值rtnBeen,并做判断。
			 */
			rtnBeen = updateDnInfo(wo_id, inTrx.getDn_no(),
					prdTotalCnt);
			if (_NORMAL != rtnBeen.getlRc()) {
				gszMsgBuf = rtnBeen.getMsgBuf();
				return rtnBeen.getlRc();
			}

			//暂时注释掉厦门天马逻辑
//			String cusId = ret_wo.getCus_id_fk();
//			boolean isXMTMProd = bisDataDao.checkEdiCusProc("XMTM",cusId);
//			if(isXMTMProd && box !=null  && !StringUtils.isEmpty(box.getPallet_id_fk())){
//				sendXmtmShippingDataProc(box.getPallet_id_fk());
//			}
		}

		return lRc;
	}

	private SimpleRtnBeen updateDnInfo(String woId, String dnNo, Integer totalCnt) throws ParseException {
		SimpleRtnBeen rtnBeen = new SimpleRtnBeen(_NORMAL);
		// RET_SHIP_NOTICE
		Ret_ship_notice ret_ship_notice = new Ret_ship_notice();
		List<Ret_ship_notice> ret_ship_notice_list = new ArrayList<Ret_ship_notice>();
		String hql = "FROM Ret_ship_notice where dn_no ='"+inTrx.getDn_no()+
					"' and evt_seq_id ='"+inTrx.getEvt_seq_id()+"'";
		Session session = retShipNoticeDao.getCrSession();
		Query query = session.createQuery(hql);
		ret_ship_notice_list = query.list();
		if (!ret_ship_notice_list.isEmpty()) {
			for(Ret_ship_notice t : ret_ship_notice_list){
				session.lock(t, LockMode.UPGRADE);
			}
			ret_ship_notice = ret_ship_notice_list.get(0);
		}else {
			gszMsgBuf = new NotFoundRtnBeen("交货订单[" + inTrx.getDn_no() + "]",
					Ret_ship_notice.class).toString();
			rtnBeen.setMsgBuf(gszMsgBuf);
			rtnBeen.setlRc(E_RET_SHIP_NOTICE + E_READ_NOT_FOUND);
			return rtnBeen;
		}
		
		if (null == ret_ship_notice) {
			gszMsgBuf = new NotFoundRtnBeen("交货订单[" + inTrx.getDn_no() + "]",
					Ret_ship_notice.class).toString();
			rtnBeen.setMsgBuf(gszMsgBuf);
			rtnBeen.setlRc(E_RET_SHIP_NOTICE + E_READ_NOT_FOUND);
			return rtnBeen;
		}

		if (PROC_HOLD.equals(ret_ship_notice.getDn_stat())) {
			gszMsgBuf = "交货订单 [" + inTrx.getDn_no() + "] 处于锁定状态，不允许出货!";
			rtnBeen.setMsgBuf(gszMsgBuf);
			rtnBeen.setlRc(E_XPWMSOPE_SHIP_NOTICE_HOLD);
			return rtnBeen;
		}
		if (PROC_COMPLETE.equals(ret_ship_notice.getDn_stat())) {
			gszMsgBuf = "交货订单 [" + inTrx.getDn_no() + "] 已经出货完毕!";
			rtnBeen.setMsgBuf(gszMsgBuf);
			rtnBeen.setlRc(E_XPWMSOPE_SHIP_NOTICE_COMP);
			return rtnBeen;
		}

		if (!PROC_WAIT.equals(ret_ship_notice.getDn_stat())) {
			gszMsgBuf = "交货订单 [" + inTrx.getDn_no() + "] 状态 ["
					+ ret_ship_notice.getDn_stat() + "] 异常，必须是 [" + PROC_WAIT
					+ "]， 请确认!";
			rtnBeen.setMsgBuf(gszMsgBuf);
			rtnBeen.setlRc(E_XPWMSOPE_SHIP_NOTICE_NOT_WAIT);
			return rtnBeen;
		}

		if (ret_ship_notice.getWh_out_prd_qty() + totalCnt > ret_ship_notice
				.getShip_qty()) {
			gszMsgBuf = "交货订单 [" + inTrx.getDn_no() + "] 已经出货数量 ["
					+ ret_ship_notice.getWh_out_prd_qty() + "]，交货数量 ["
					+ ret_ship_notice.getShip_qty() + "]， 不够本次出货数量!";
			rtnBeen.setMsgBuf(gszMsgBuf);
			rtnBeen.setlRc(E_XPWMSOPE_DN_NOT_ENOUGH);
			return rtnBeen;
		}
		ret_ship_notice.setWh_out_prd_qty(ret_ship_notice.getWh_out_prd_qty()
				+ totalCnt);
		if (ret_ship_notice.getWh_out_prd_qty().equals(
				ret_ship_notice.getShip_qty())) {
			ret_ship_notice.setDn_stat(PROC_COMPLETE);
		}
		ret_ship_notice.setShip_usr(inTrx.getRelate_usr());
		ret_ship_notice.setShip_car_id(inTrx.getPalte_no());
		ret_ship_notice.setEvt_timestamp(gEvtTimestamp);
		ret_ship_notice.setShipping_timestamp(gEvtTimestamp);// Update
		ret_ship_notice.setEvt_cate(EVT_WHOUT);

		retShipNoticeDao.update(ret_ship_notice);

		return rtnBeen;
	}

	/**
	 * Set Ret_box Nx Info
	 * 
	 * @param box
	 * @param nx_ope_id
	 * @param nx_ope_no
	 * @param nx_ope_ver
	 * @param nx_proc_id
	 * @param nx_tool_Group
	 */
	private void setBoxNxInf(Ret_box box, String nx_ope_id, String nx_ope_no,
			String nx_ope_ver, String path_id, String path_ver,
			String nx_proc_id, String nx_tool_Group, String final_wo_id) {
		box.setBox_stat(PROC_WAIT);
		box.setDest_shop(_SPACE);
		box.setNx_ope_id_fk(nx_ope_id);
		box.setNx_ope_no_fk(nx_ope_no);
		box.setNx_ope_ver_fk(nx_ope_ver);
		box.setNx_path_id_fk(path_id);
		box.setNx_path_ver_fk(path_ver);
		box.setNx_proc_id_fk(nx_proc_id);
		box.setNx_toolg_id_fk(nx_tool_Group);
		box.setBnk_flg(BANK_OUT);
		box.setEvt_cate(EVT_WH_RETURN);
		box.setEvt_timestamp(gEvtTimestamp);
		box.setEvt_time_d(gEvtTimeD);
		box.setEvt_usr(inTrx.getEvt_user());
		if (!box.getWo_id_fk().equals(final_wo_id)) {
			box.setPrd_admin_flg("NNNNNNNNNNNNNNNNNNNN");			
		}
		box.setWo_id_fk(final_wo_id);
	}

	/**
	 * set Ret_prd_inf Nx Info
	 * 
	 * @param prdInf
	 * @param nx_ope_id
	 * @param nx_ope_no
	 * @param nx_ope_ver
	 * @param path_id
	 * @param path_ver
	 * @param nx_proc_id
	 * @param nx_tool_Group
	 */
	private void setPrdNxInf(Ret_prd_info prdInf, String nx_ope_id,
			String nx_ope_no, String nx_ope_ver, String path_id,
			String path_ver, String nx_proc_id, String nx_tool_Group,
			String final_wo_id) {
		prdInf.setPrd_stat(PROC_WAIT);
		prdInf.setDest_shop(_SPACE);
		prdInf.setNx_ope_id_fk(nx_ope_id);
		prdInf.setNx_ope_no_fk(nx_ope_no);
		prdInf.setNx_ope_ver_fk(nx_ope_ver);
		prdInf.setNx_path_id_fk(path_id);
		prdInf.setNx_path_ver_fk(path_ver);
		prdInf.setNx_proc_id_fk(nx_proc_id);
		prdInf.setNx_toolg_id_fk(nx_tool_Group);
		prdInf.setBnk_flg(BANK_OUT);
		prdInf.setEvt_cate(EVT_WH_RETURN);
		prdInf.setEvt_timestamp(gEvtTimestamp);
		prdInf.setEvt_time_d(gEvtTimeD);
		prdInf.setEvt_usr(inTrx.getEvt_user());
		if (!prdInf.getWo_id_fk().equals(final_wo_id)) {
			prdInf.setCus_sn(_NO1_50);
			prdInf.setPrd_admin_flg("NNNNNNNNNNNNNNNNNNNN");			
		}
		prdInf.setWo_id_fk(final_wo_id);
	}

	/**
	 * Out Return Update ret_box Info
	 * 
	 * @param ret_box
	 * @param ret_wo
	 * @param box_id
	 * @param prd_qty
	 * @param path_id
	 * @param path_ver
	 * @param nx_ope_id
	 * @param nx_ope_no
	 * @param nx_ope_ver
	 * @param nx_tool_Group
	 * @param nx_proc_id
	 * @param evt_usr
	 */
	private void updateBoxNxInfoProc(Ret_box ret_box, Ret_wo ret_wo,
			String box_id, Integer prd_qty, String path_id, String path_ver,
			String nx_ope_id, String nx_ope_no, String nx_ope_ver,
			String nx_tool_Group, String nx_proc_id, String evt_usr) {
		ret_box.setBox_id(box_id);
		ret_box.setBox_stat(PROC_WAIT);
		ret_box.setBnk_flg(BANK_OUT);
		ret_box.setStd_qty(prd_qty);
		ret_box.setPrd_qty(prd_qty);
		ret_box.setWo_id_fk(ret_wo.getWo_id());
		ret_box.setMdl_id_fk(ret_wo.getMdl_id_fk());
		ret_box.setSo_id_fk(ret_wo.getSo_id());
		ret_box.setNx_ope_id_fk(nx_ope_id);
		ret_box.setNx_ope_no_fk(nx_ope_no);
		ret_box.setNx_ope_ver_fk(nx_ope_ver);
		ret_box.setNx_path_id_fk(path_id);
		ret_box.setNx_path_ver_fk(path_ver);
		ret_box.setNx_proc_id_fk(nx_proc_id);
		ret_box.setNx_toolg_id_fk(nx_tool_Group);
		ret_box.setEvt_cate(EVT_OUT_RETURN);
		ret_box.setEvt_usr(evt_usr);
		ret_box.setEvt_time_d(gEvtTimeD);
		ret_box.setEvt_timestamp(gEvtTimestamp);
		ret_box.setPallet_id_fk(_SPACE);
		ret_box.setDn_no_fk(_SPACE);
		ret_box.setValid_flg(_YES);
	}

	/**
	 * Out Return Update ret_prd_info Info
	 * 
	 * @param ret_prd_info
	 * @param ret_wo
	 * @param ret_prd_in
	 * @param slot_no
	 * @param box_id
	 * @param path_id
	 * @param path_ver
	 * @param nx_ope_id
	 * @param nx_ope_no
	 * @param nx_ope_ver
	 * @param nx_tool_Group
	 * @param nx_proc_id
	 * @param evt_usr
	 */
	private void updatePrdNxInfProc(Ret_prd_info ret_prd_info, Ret_wo ret_wo,
			Ret_prd_in ret_prd_in, String slot_no, String box_id,
			String path_id, String path_ver, String nx_ope_id,
			String nx_ope_no, String nx_ope_ver, String nx_tool_Group,
			String nx_proc_id, String evt_usr, Integer prd_qty) {
		ret_prd_info.setSlot_no(slot_no);
		ret_prd_info.setPv_box_id_fk(ret_prd_info.getBox_id_fk());
		ret_prd_info.setPv_prd_stat_fk(ret_prd_info.getPrd_stat());
		ret_prd_info.setBox_id_fk(box_id);
		ret_prd_info.setPrd_stat(PROC_WAIT);
		ret_prd_info.setBnk_flg(BANK_OUT);
		ret_prd_info.setWo_id_fk(ret_wo.getWo_id());
		ret_prd_info.setMdl_id_fk(ret_wo.getMdl_id_fk());
		ret_prd_info.setSo_id_fk(ret_wo.getSo_id());
		ret_prd_info.setNx_ope_id_fk(nx_ope_id);
		ret_prd_info.setNx_ope_no_fk(nx_ope_no);
		ret_prd_info.setNx_ope_ver_fk(nx_ope_ver);
		ret_prd_info.setNx_path_id_fk(path_id);
		ret_prd_info.setNx_path_ver_fk(path_ver);
		ret_prd_info.setNx_proc_id_fk(nx_proc_id);
		ret_prd_info.setNx_toolg_id_fk(nx_tool_Group);

		ret_prd_info.setMtrl_box_id_fk(ret_prd_in.getId().getMtrl_box_id());
		ret_prd_info.setMtrl_prod_id_fk(ret_prd_in.getMtrl_prod_id_fk());

		ret_prd_info.setEvt_timestamp(gEvtTimestamp);
		ret_prd_info.setEvt_time_d(gEvtTimeD);
		ret_prd_info.setEvt_cate(EVT_OUT_RETURN);
		ret_prd_info.setEvt_usr(evt_usr);
		ret_prd_info.setPallet_id(_SPACE);
		ret_prd_info.setDn_no(_SPACE);
		ret_prd_info.setShip_box_id_fk(_SPACE);
		ret_prd_info.setPrd_qty(prd_qty);
		ret_prd_info.setPrd_std_qty(prd_qty);
		if (inTrx.getAction_flg().equals("L")) {
			ret_prd_info.setLot_id(_SPACE);
			ret_prd_info.setEvt_seq_id(GUIDGenerator.javaGUID());
			ret_prd_info.setUnq_seq_id(GUIDGenerator.javaGUID());
		}
	}

	/**
	 * Out Return Check Qty info
	 * 
	 * @param prd_qty
	 * @param ret_prd_in
	 * @param ret_wo
	 * @return
	 */
	private long checkQtyInf(Integer prd_qty, Ret_prd_in ret_prd_in,
			Ret_wo ret_wo) {
		long lRc = _NORMAL;
		if (!ret_prd_in.getPrd_tl_qty().equals(ret_prd_in.getPrd_act_qty())) {
			gszMsgBuf = "来料箱号没有全部发料，请检查！";
			return E_MPWMSOPE_INVALID_QTY;
		}
		if (!prd_qty.equals(ret_prd_in.getPrd_tl_qty())) {
			gszMsgBuf = "返工的数量与来料箱号的总数量不等，请检查！";
			return E_MPWMSOPE_INVALID_QTY;
		}
		if (!ret_prd_in.getPpbox_stat().equals(PROC_WFRL)) {
			gszMsgBuf = "来料箱号没有发料，请检查！";
			return E_MPWMSOPE_INVALID_QTY;
		}
		if (ret_wo.getRl_prd_qty() + prd_qty > ret_wo.getPln_prd_qty()) {
			gszMsgBuf = "返工的数量加上此工单已经下线的数量大于工单的计划数量，请检查！";
			return E_MPWMSOPE_INVALID_QTY;
		}
		if (!ret_wo.getWo_id().equals(ret_prd_in.getWo_id_fk())) {
			gszMsgBuf = "来料箱号上的内部订单与所选订单不同，请检查！";
			return E_MPWMSOPE_INVALID_QTY;
		}
		return lRc;
	}

	/**
	 * Out Return Check Wo info
	 * 
	 * @param ret_wo
	 * @return
	 */
	private long checkWoInf(Ret_wo ret_wo) {
		long lRc = _NORMAL;
		if (ret_wo.getWo_stat().equals(WO_STATUS_CLOS)) {
			gszMsgBuf = "此内部订单已经关闭，不可做返工！";
			return E_MPWMSOPE_INVALID_STAT;
		}
		if (ret_wo.getPath_id_fk().equals("") || ret_wo.getPath_id_fk() == null) {
			gszMsgBuf = "内部工单上没有对应的工艺路线，不可做返工！";
			return E_MPWMSOPE_NOT_FOUND_PATH;
		}
		if (ret_wo.getStr_ope_no().equals("") || ret_wo.getStr_ope_no() == null) {
			gszMsgBuf = "内部工单上没有对应的起始站点，不可做返工！";
			return E_MPWMSOPE_NOT_FOUND_START_OPE;
		}
		return lRc;
	}

	/**
	 * Out Return Check Box info
	 * 
	 * @param box_id
	 * @param cus_id
	 */
	private long checkBoxInf(Ret_prd_in ret_prd_in, String box_id, String cus_id) {
		long lRc = _NORMAL;
		if (ret_prd_in == null) {
			gszMsgBuf = "此箱号没有做收料，不可做返工！";
			return E_RET_PRD_IN + E_READ_NOT_FOUND;
		}
		if (ret_prd_in.getPpbox_stat().equals(PROC_WAIT)) {
			gszMsgBuf = "此箱号没有做发料，不可做返工！";
			return E_MPWMSOPE_INVALID_STAT;
		}
		if (ret_prd_in.getPpbox_stat().equals(PROC_EMPTY)) {
			gszMsgBuf = "此箱号已经全部下线，不可做返工！";
			return E_MPWMSOPE_INVALID_STAT;
		}
		Ret_box box = retBoxDao.get(box_id);
		if (box != null) {
			if (box.getBox_stat().equals(PROC_SHTC)) {
				gszMsgBuf = "此箱号存在于产线中，且不是出货状态，请检查！";
				return E_MPWMSOPE_INVALID_STAT;
			}
		}
		return lRc;
	}

	private SimpleRtnBeen getCurMdl(Ret_box box, Ret_wo ret_wo,
			boolean isWipDestShop) {
		SimpleRtnBeen rtnBeen = new SimpleRtnBeen(_NORMAL);

		String curMdlCate = "";

		if (null == ret_wo) {
			ret_wo = retWODao.getWithLock(box.getWo_id_fk(), LockMode.UPGRADE);
			if (null == ret_wo) {
				gszMsgBuf = new NotFoundRtnBeen(box.getWo_id_fk(), Ret_wo.class)
						.toString();
				rtnBeen.setlRc(E_RET_WO + E_READ_NOT_FOUND);
				rtnBeen.setMsgBuf(gszMsgBuf);
				return rtnBeen;
			}

			if (Util.isSpaceCheck(ret_wo.getMdl_cate_fk())) {
				gszMsgBuf = "内部订单维护错误，工艺路线类型不能为空！";
				rtnBeen.setMsgBuf(gszMsgBuf);
				rtnBeen.setlRc(E_XPWMSOPE_INVALID_WO_MDL_CATE);
				return rtnBeen;
			}
		}

		if (isWipDestShop) {
			Bis_path cr_path = new Bis_path();
			Bis_pathId cr_path_id = new Bis_pathId();
			Bis_path pv_path = new Bis_path();
			Bis_pathId pv_path_id = new Bis_pathId();
			Bis_ope pv_ope = new Bis_ope();
			Bis_opeId pv_ope_id = new Bis_opeId();

			cr_path_id.setCr_ope_no(box.getNx_ope_no_fk());
			cr_path_id.setPath_id_fk(box.getNx_path_id_fk());
			cr_path_id.setPath_ver_fk(box.getNx_path_ver_fk());

			cr_path = bisPathDao.get(cr_path_id);

			String pv_ope_no = cr_path.getPv_ope_no();
			if (PATH_FRST_OPE.equals(pv_ope_no)) {
				curMdlCate = ret_wo.getMdl_cate_fk().substring(0, 1);
			} else {
				pv_path_id.setPath_id_fk(box.getNx_path_id_fk());
				pv_path_id.setPath_ver_fk(box.getNx_path_ver_fk());
				pv_path_id.setCr_ope_no(pv_ope_no);

				pv_path = bisPathDao.get(pv_path_id);
				if (null == pv_path) {
					gszMsgBuf = new NotFoundRtnBeen(pv_path_id.toString(),
							Bis_path.class).toString();
					rtnBeen.setlRc(E_BIS_PATH + E_READ_NOT_FOUND);
					rtnBeen.setMsgBuf(gszMsgBuf);
					return rtnBeen;
				}

				pv_ope_id.setOpe_id(pv_path.getCr_ope_id_fk());
				pv_ope_id.setOpe_ver(pv_path.getCr_ope_ver_fk());

				pv_ope = bisOpeDao.get(pv_ope_id);
				if (null == pv_ope) {
					gszMsgBuf = new NotFoundRtnBeen(pv_ope_id.toString(),
							Bis_ope.class).toString();
					rtnBeen.setlRc(E_BIS_OPE + E_READ_NOT_FOUND);
					rtnBeen.setMsgBuf(gszMsgBuf);
					return rtnBeen;
				}

				Bis_data PRST_data = new Bis_data();
				String querySql = "FROM Bis_data where data_cate = 'PRST' AND data_id = ? ";
				PRST_data = bisDataDao.uniqueResult(querySql,
						pv_ope.getProc_id());
				curMdlCate = PRST_data.getExt_4();
			}

		} else {
			curMdlCate = ret_wo.getMdl_cate_fk().substring(
					ret_wo.getMdl_cate_fk().length() - 1,
					ret_wo.getMdl_cate_fk().length());
		}

		rtnBeen.setObj(curMdlCate);

		return rtnBeen;
	}

	private SimpleRtnBeen addTableForSHTC(Ret_box ret_box,
			List<Ret_prd_info> prdInfoList , Ret_wo ret_wo) {
		SimpleRtnBeen rtnBeen = new SimpleRtnBeen(_NORMAL);
		// add or update RET_BOX_SHTC
		Ret_box_shtc ret_box_shtc = retBoxShtcDao.get(ret_box.getBox_id());
		if (ret_box_shtc == null) {
			ret_box_shtc = new Ret_box_shtc();
			ret_box_shtc.setBox_id(ret_box.getBox_id());
		}
		ret_box_shtc.setUnq_seq_id(gEvtSeqID);
		ret_box_shtc.setPrd_grade_fk(ret_box.getPrd_grade_fk());
		ret_box_shtc.setStd_qty(ret_box.getStd_qty());
		ret_box_shtc.setPrd_qty(ret_box.getPrd_qty());
		ret_box_shtc.setMdl_id_fk(ret_box.getMdl_id_fk());
		ret_box_shtc.setMtrl_prod_id_fk(ret_box.getMtrl_prod_id_fk());
		ret_box_shtc.setWo_id_fk(ret_box.getWo_id_fk());
		ret_box_shtc.setDest_shop(ret_box.getDest_shop());
		ret_box_shtc.setSo_id_fk(ret_box.getSo_id_fk());
		ret_box_shtc.setShip_box_id(ret_box.getShip_box_id());
		ret_box_shtc.setShipping_timestamp(ret_box.getShipping_timestamp());
		ret_box_shtc.setBnk_flg(ret_box.getBnk_flg());
		retBoxShtcDao.saveOrUpdate(ret_box_shtc);

		// add or update RET_PRD_INFO_SHTC
		List<Ret_prd_info_shtc> prdInfoSthcList = new ArrayList<Ret_prd_info_shtc>();
		for (int i = 0; i < prdInfoList.size(); i++) {
			Ret_prd_info_shtc prdInfoShtc = retPrdInfoShtcDao.get(prdInfoList
					.get(i).getPrd_seq_id());
			if (prdInfoShtc == null) {
				prdInfoShtc = new Ret_prd_info_shtc();
				prdInfoShtc.setPrd_seq_id(prdInfoList.get(i).getPrd_seq_id());
			}
			prdInfoShtc.setUnq_seq_id(gEvtSeqID);
			prdInfoShtc.setEvt_seq_id(gEvtSeqID);
			prdInfoShtc.setPrd_seq_typ(prdInfoList.get(i).getPrd_seq_typ());
			prdInfoShtc.setBox_id_fk(prdInfoList.get(i).getBox_id_fk());
			prdInfoShtc.setSlot_no(prdInfoList.get(i).getSlot_no());
			prdInfoShtc.setMdl_id_fk(prdInfoList.get(i).getMdl_id_fk());
			prdInfoShtc.setPv_prd_seq_id(prdInfoList.get(i).getPv_prd_seq_id());
			prdInfoShtc.setPrd_qty(prdInfoList.get(i).getPrd_qty());
			prdInfoShtc.setPrd_std_qty(prdInfoList.get(i).getPrd_std_qty());
			prdInfoShtc.setPrd_grade(prdInfoList.get(i).getPrd_grade());
			prdInfoShtc.setPrd_thickness(prdInfoList.get(i).getPrd_thickness());
			prdInfoShtc.setLayot_id_fk(prdInfoList.get(i).getLayot_id_fk());
			prdInfoShtc.setX_axis_cnt_fk(prdInfoList.get(i).getX_axis_cnt_fk());
			prdInfoShtc.setY_axis_cnt_fk(prdInfoList.get(i).getY_axis_cnt_fk());
			prdInfoShtc.setFab_sn(prdInfoList.get(i).getFab_sn());
			prdInfoShtc.setWo_id_fk(prdInfoList.get(i).getWo_id_fk());
			prdInfoShtc.setSo_id_fk(prdInfoList.get(i).getSo_id_fk());
			prdInfoShtc.setMtrl_prod_id_fk(prdInfoList.get(i)
					.getMtrl_prod_id_fk());
			prdInfoShtc.setMtrl_box_id_fk(prdInfoList.get(i)
					.getMtrl_box_id_fk());
			prdInfoShtc.setDest_shop(prdInfoList.get(i).getDest_shop());
			prdInfoShtc.setBnk_flg(prdInfoList.get(i).getBnk_flg());
			prdInfoShtc.setShipping_timestamp(prdInfoList.get(i)
					.getShipping_timestamp());
			prdInfoShtc.setShip_box_id_fk(prdInfoList.get(i)
					.getShip_box_id_fk());
			prdInfoShtc.setMtrl_slot_no_fk(prdInfoList.get(i).getMtrl_slot_no_fk());
			prdInfoShtc.setMdl_cate_fk(prdInfoList.get(i).getMdl_cate_fk());
			prdInfoShtc.setLot_id(prdInfoList.get(i).getLot_id());
			prdInfoShtc.setLot_splt_id(prdInfoList.get(i).getLot_splt_id());
			prdInfoShtc.setCus_id_fk(ret_wo.getCus_id_fk());
			prdInfoSthcList.add(prdInfoShtc);
		}
		retPrdInfoShtcDao.saveOrUpdateAll(prdInfoSthcList);

		return rtnBeen;
	}

	/**
	 * 同步DPS信息 4300 & 4A00使用
	 * 
	 * @param wo_id
	 * @param diffCnt
	 * @return
	 */
	private SimpleRtnBeen syncDpsInfoForWHInMOS(String wo_id, Integer diffCnt, Ret_box box,Ret_prd_info ret_prd_info, boolean isWipIn) {

		SimpleRtnBeen rtnBeen = new SimpleRtnBeen(_NORMAL);
//		rtnBeen = retWODpsDao.syncDpsInfoForWHInMOS(wo_id, diffCnt, box, ret_prd_info, isWipIn);
//		String curMdlCate = "";
//		
//		Ret_wo ret_wo = new Ret_wo();
//		ret_wo = retWODao.get(wo_id);
//		if (null == ret_wo) {
//			gszMsgBuf = new NotFoundRtnBeen(wo_id, Ret_wo.class).toString();
//			rtnBeen.setlRc(E_RET_WO + E_READ_NOT_FOUND);
//			rtnBeen.setMsgBuf(gszMsgBuf);
//			return rtnBeen;
//		}
//		
//		if (isWipIn) {
//			Bis_path cr_path = new Bis_path();
//			Bis_pathId cr_path_id = new Bis_pathId();
//
//			cr_path_id.setCr_ope_no(box.getNx_ope_no_fk());
//			cr_path_id.setPath_id_fk(box.getNx_path_id_fk());
//			cr_path_id.setPath_ver_fk(box.getNx_path_ver_fk());
//
//			cr_path = bisPathDao.get(cr_path_id);
//
//			String pv_ope_no = cr_path.getPv_ope_no();
//			if (PATH_FRST_OPE.equals(pv_ope_no)) {
//				curMdlCate = ret_wo.getMdl_cate_fk().substring(0, 1);
//			} else {
//				Bis_data cr_proc = new Bis_data();
//				Bis_data nx_proc = new Bis_data();
//				String querySql = "FROM Bis_data where data_cate = 'PRST' AND data_id = ? ";
//				cr_proc = bisDataDao.uniqueResult(querySql,ret_prd_info.getCr_proc_id_fk());
//				nx_proc = bisDataDao.uniqueResult(querySql,ret_prd_info.getNx_proc_id_fk());
//				if(cr_proc.getExt_4().equals(nx_proc.getExt_4())){
//					return rtnBeen;
//				}else{
//					curMdlCate = cr_proc.getExt_4();
//				}
//			}
//
//		} else {
//			curMdlCate = ret_wo.getMdl_cate_fk().substring(ret_wo.getMdl_cate_fk().length() - 1,ret_wo.getMdl_cate_fk().length());
//		}
//			
//		WoDpsInfoBeen woDpsInfoBeen = new WoDpsInfoBeen();
//		woDpsInfoBeen = retWODpsDao.getWoDpsInfoByWoIdMOS(ret_wo, curMdlCate);
//		if (woDpsInfoBeen.isHas_lvl1_dps()) {
//			// 有一级计划
//			if (woDpsInfoBeen.isMix_lvl1_dps()) {//跨一级计划
//				
//			} else { // Start of 不跨一级计划
//				Ret_wo_dps lvl1_dps = retWODpsDao.getWithLock(woDpsInfoBeen.getDps_list_1().get(0).getEvt_seq_id(),	LockMode.UPGRADE);
//				// 开始计算
//				List<Ret_wo_dps> dps_list_2 = new ArrayList<Ret_wo_dps>();
//				dps_list_2 = woDpsInfoBeen.getDps_list_2();
//				
//				Ret_wo_dps first2Dps = null;
//				
//				for (Ret_wo_dps dps2 : dps_list_2) {
//					if (wo_id.equals(dps2.getWo_id_fk())) {
//						first2Dps = retWODpsDao.getWithLock(dps2.getEvt_seq_id(),LockMode.UPGRADE);
//						
//						retWODpsDao.syncDps2CntWHIn(ret_wo, lvl1_dps, first2Dps,null, diffCnt);
//						break;
//					}
//				}
//			}// End of 不跨一级计划
//		} else {
//			List<Ret_wo_dps> dps_list_2 = new ArrayList<Ret_wo_dps>();
//			dps_list_2 = woDpsInfoBeen.getDps_list_2();
//			
//			Ret_wo_dps first2Dps = null;
//			
//			for (Ret_wo_dps dps2 : dps_list_2) {
//				if (wo_id.equals(dps2.getWo_id_fk())) {
//					first2Dps = retWODpsDao.getWithLock(dps2.getEvt_seq_id(),LockMode.UPGRADE);
//					retWODpsDao.syncDps2NoDps1CntWHIn(ret_wo, null, first2Dps,null, diffCnt);
//					break;
//				}
//			}
//		}// End of 没有一级计划			
		return rtnBeen;

	}	
	
	/**
	 * whouse change destshop operation
	 * 
	 * @return
	 */
	private long whChgX() {
		long lRc = _NORMAL;
		
		String sql = "FROM Bis_data where data_cate = 'DEST' and data_id = '" + inTrx.getDest_shop() + "'" ;
		List<Bis_data> shopList = new ArrayList<Bis_data>();
		shopList = bisDataDao.find(sql);
		if(shopList.isEmpty()){
			gszMsgBuf = "仓别[" + inTrx.getDest_shop() + "]不存在！";
			return E_READ_NOT_FOUND;
		}
		if (inTrx.getBox_cnt() > 0) {
			for (XpwmsopeIA iary : inTrx.getIary()) {

				// Get box
				Ret_box box = new Ret_box();
				box = retBoxDao.getWithLock(iary.getBox_id(), LockMode.UPGRADE);
				if (null == box) {
					gszMsgBuf = "箱子 [" + iary.getBox_id() + "] 不存在 ["
							+ Ret_box.class.getSimpleName().toUpperCase()
							+ "]!";
					return E_RET_BOX + E_READ_NOT_FOUND;
				}

				// Check box info
				if (!PROC_SHIP.equals(box.getBox_stat())) {
					gszMsgBuf = "转仓时箱子状态必须是 SHIP, [" + box.getBox_id()
							+ "] 当前状态是 [" + box.getBox_stat() + "]";
					return INVALIED_BOX_STAT;
				}
				if (!inTrx.getFr_dest_shop().equals(box.getDest_shop())) {
					gszMsgBuf = "当前仓别["+inTrx.getFr_dest_shop()+"]与准备转仓的箱子["+box.getBox_id()+"]的仓别["+box.getDest_shop()+"]不同，不可转仓！";
					return E_XPWMSOPE_SAME_DEST_SHOP;
				}
				if (inTrx.getDest_shop().equals(box.getDest_shop())) {
					gszMsgBuf = "仓别相同，不可转仓！";
					return E_XPWMSOPE_SAME_DEST_SHOP;
				}
				
				if(box.getBnk_flg().equals("4")){
					gszMsgBuf = "已经在转仓中，请先收货再转仓！";
					return INVALIED_BOX_STAT;
				}

				// Fetch panels
				List<Ret_prd_info> prdInfoList = new ArrayList<Ret_prd_info>();
				prdInfoList = retPrdInfoDao.getShtListInfo(iary.getBox_id());
				if (null != prdInfoList) {
					if (box.getPrd_qty() == prdInfoList.size()) {
						//end
						for (Ret_prd_info item : prdInfoList) {
							if (!PROC_SHIP.equals(item.getPrd_stat())) {
								gszMsgBuf = "转仓时玻璃状态必须是 SHIP, ["
										+ item.getPrd_seq_id() + "] 当前状态是 ["
										+ item.getPrd_stat() + "]";
								return INVALIED_SHT_STAT;
							}
							item.setEvt_cate("WHCX");
							item.setDest_shop("ZCW"+box.getBnk_flg());
							item.setBnk_flg("4");
							item.setEvt_usr(inTrx.getEvt_user());
							item.setEvt_timestamp(gEvtTimestamp);
							item.setEvt_time_d(gEvtTimeD);
							retPrdInfoDao.update(item);
						}
					} else {
						gszMsgBuf = "Box [" + iary.getBox_id() + "] has ["
								+ iary.getPrd_qty()
								+ "] panels, but only find ["
								+ prdInfoList.size() + "].";
						return INVALIED_SHT_CNT;
					}
				} else {
					gszMsgBuf = "No panel in box [" + iary.getBox_id() + "].";
					return E_XPWMSOPE_FIND_NO_PANEL_BY_BOX;
				}
				box.setPv_prty(box.getDest_shop());
				box.setDest_shop("ZCW"+box.getBnk_flg());
				box.setEvt_cate("WHCX");
                box.setBox_def(inTrx.getDest_shop());
                box.setBnk_flg("4");
				box.setEvt_usr(inTrx.getEvt_user());
				box.setEvt_timestamp(gEvtTimestamp);
				box.setEvt_time_d(gEvtTimeD);
				retBoxDao.update(box);
			}
		}
		return lRc;
	}
	
	/**
	 * whouse change destshop operation
	 * 
	 * @return
	 */
	private long whChgY() {
		long lRc = _NORMAL;
		String bnkflg;
		if (inTrx.getBox_cnt() > 0) {
			for (XpwmsopeIA iary : inTrx.getIary()) {

				// Get box
				Ret_box box = new Ret_box();
				box = retBoxDao.getWithLock(iary.getBox_id(), LockMode.UPGRADE);
				if (null == box) {
					gszMsgBuf = "箱子 [" + iary.getBox_id() + "] 不存在 ["
							+ Ret_box.class.getSimpleName().toUpperCase()
							+ "]!";
					return E_RET_BOX + E_READ_NOT_FOUND;
				}
				
				// Check box info
				if (!PROC_SHIP.equals(box.getBox_stat())) {
					gszMsgBuf = "转仓时箱子状态必须是 SHIP, [" + box.getBox_id()
							+ "] 当前状态是 [" + box.getBox_stat() + "]";
					return INVALIED_BOX_STAT;
				}
				//check fab
				String hqldest = "FROM Bis_data WHERE data_cate = 'DEST' AND data_ext in ('F','E') AND data_id = '" + box.getBox_def() + "'"; 
				List<Bis_data> dataList = bisDataDao.find(hqldest);
				if(dataList.size() == 0){
					gszMsgBuf = "箱子["+iary.getBox_id()+"]仓别["+box.getBox_def()+"]在bis_data中不存在";
					return E_BIS_DATA + E_READ_NOT_FOUND;
				}
				//bak_flg
				bnkflg=box.getDest_shop().substring(3, 4);

				// Fetch panels
				List<Ret_prd_info> prdInfoList = new ArrayList<Ret_prd_info>();
				prdInfoList = retPrdInfoDao.getShtListInfo(iary.getBox_id());
				if (null != prdInfoList) {
					if (box.getPrd_qty() == prdInfoList.size()) {
						for (Ret_prd_info item : prdInfoList) {
							if (!PROC_SHIP.equals(item.getPrd_stat())) {
								gszMsgBuf = "转仓时玻璃状态必须是 SHIP, ["
										+ item.getPrd_seq_id() + "] 当前状态是 ["
										+ item.getPrd_stat() + "]";
								return INVALIED_SHT_STAT;
							}

							item.setEvt_cate("WHCY");
							item.setDest_shop(box.getBox_def());
							item.setBnk_flg(bnkflg);
						    item.setEvt_usr(inTrx.getEvt_user());
							item.setEvt_timestamp(gEvtTimestamp);
							item.setEvt_time_d(gEvtTimeD);
							retPrdInfoDao.update(item);
						}
					} else {
						gszMsgBuf = "Box [" + iary.getBox_id() + "] has ["
								+ iary.getPrd_qty()
								+ "] panels, but only find ["
								+ prdInfoList.size() + "].";
						return INVALIED_SHT_CNT;
					}
				} else {
					gszMsgBuf = "No panel in box [" + iary.getBox_id() + "].";
					return E_XPWMSOPE_FIND_NO_PANEL_BY_BOX;
				}
				box.setEvt_cate("WHCY");
				box.setDest_shop(box.getBox_def());
				box.setBox_def("");
				box.setPv_prty("");
				box.setBnk_flg(bnkflg);
				box.setEvt_usr(inTrx.getEvt_user());
				box.setEvt_timestamp(gEvtTimestamp);
				box.setEvt_time_d(gEvtTimeD);
				retBoxDao.update(box);
			}
		}

		return lRc;
	}
	
	/**
	 * whouse shipping out operation
	 * 
	 * @return
	 * @throws ParseException 
	 */
	private long whOutbydn() throws ParseException {
		long lRc = _NORMAL;
		Ret_box box;
		Ret_ship_notice ret_ship_notice;
		List<Ret_prd_info> prdInfoList;
		Ret_wo ret_wo;
		SimpleRtnBeen rtnBeen = new SimpleRtnBeen(_NORMAL);
		lRc = checkBoxPallet();
		if (lRc != _NORMAL) {
			return lRc;
		}
		ret_wo = new Ret_wo();
		ret_wo = retWODao.getWithLock(inTrx.getWo_id(),
				LockMode.UPGRADE);
		if (null == ret_wo) {
			gszMsgBuf = new NotFoundRtnBeen(inTrx.getWo_id(),
					Ret_wo.class).toString();
			return E_RET_WO + E_READ_NOT_FOUND;
		}

		Set<String> palletIdSet = new HashSet<String>();
		for (XpwmsopeIA iary : inTrx.getIary()) {
			box = new Ret_box();
			box = retBoxDao.getWithLock(iary.getBox_id(), LockMode.UPGRADE);
			if (null == box) {
				gszMsgBuf = "箱子[" + iary.getBox_id() + "] 不存在 ["
						+ Ret_box.class.getSimpleName().toUpperCase()
						+ "]!";
				return E_RET_BOX + E_READ_NOT_FOUND;
			}

			palletIdSet.add(box.getPallet_id_fk());
			if(!PROC_SHIP.equals(box.getBox_stat())){
				gszMsgBuf = "箱子[" + iary.getBox_id() + "] 状态不对，非在库状况 ["
						+ box.getBox_stat()	+ "]!";
				return E_RET_BOX + E_READ_NOT_FOUND;
			}
			
			String prd_gradeNG = box.getPrd_grade_fk();
			// 原箱check
			// 非OK品不做此check  -- 2015.01.26
//			if (_OK.equals(prd_gradeNG)) {
//				if (_YES.equals(ret_wo.getOpe_eff_flg())) {
//					rtnBeen = retBoxDao.chkMtrlBoxAndSlot(box, true);
//					if (_NORMAL != rtnBeen.getlRc()) {
//						gszMsgBuf = rtnBeen.getMsgBuf();
//						return rtnBeen.getlRc();
//					}
//				}
//			}

			//totleprdcnt = totleprdcnt + box.getPrd_qty();
			for (XpwmsopeIC iaryC : inTrx.getIaryC()) {
				Integer dnleftcnt = 0;
				ret_ship_notice = new Ret_ship_notice();
				if(PROC_SHTC.equals(box.getBox_stat())){
					break;
				}
				String hql = "FROM Ret_ship_notice where evt_seq_id=?";
				ret_ship_notice = retShipNoticeDao.uniqueResultWithLock(hql,iaryC.getEvt_seq_id());
				if (null == ret_ship_notice) {
					gszMsgBuf = new NotFoundRtnBeen(iaryC.getDn_no(),
							Ret_ship_notice.class).toString();
					return E_RET_SHIP_NOTICE + E_READ_NOT_FOUND;
				}

				if(PROC_COMPLETE.equals(ret_ship_notice.getDn_stat())){
					continue;
				}
				
                dnleftcnt = ret_ship_notice.getShip_qty() - ret_ship_notice.getWh_out_prd_qty();
				if (PROC_HOLD.equals(ret_ship_notice.getDn_stat())) {
					gszMsgBuf = "交货订单 [" + inTrx.getDn_no() + "] 处于锁定状态，不允许出货!";
					return E_XPWMSOPE_SHIP_NOTICE_HOLD;
				}

				if (!PROC_WAIT.equals(ret_ship_notice.getDn_stat())) {
					gszMsgBuf = "交货订单 [" + inTrx.getDn_no() + "] 状态 ["
							+ ret_ship_notice.getDn_stat() + "] 异常，必须是 [" + PROC_WAIT
							+ "]， 请确认!";
					return E_XPWMSOPE_SHIP_NOTICE_NOT_WAIT;
				}

				prdInfoList = new ArrayList<Ret_prd_info>();
				String hqlsht = "FROM Ret_prd_info where box_id_fk = ? and dn_no = null";
				prdInfoList = retPrdInfoDao.listWithLock(hqlsht, iary.getBox_id());
				
				if(prdInfoList.size()<=dnleftcnt){
					for (Ret_prd_info item : prdInfoList) {
						if (!PROC_SHIP.equals(item.getPrd_stat())) {
							gszMsgBuf = "出货的玻璃状态必须是 SHIP, ["
									+ item.getPrd_seq_id() + "] 当前状态是 ["
									+ item.getPrd_stat() + "]";
							return INVALIED_SHT_STAT;
						}

						item.setEvt_cate(EVT_WHOUT);
						item.setPrd_stat(PROC_SHTC);
						item.setEvt_usr(inTrx.getEvt_user());
						item.setEvt_timestamp(gEvtTimestamp);
						item.setEvt_time_d(gEvtTimeD);
						item.setShipping_timestamp(gEvtTimestamp);
						item.setShip_box_id_fk(box.getShip_box_id());
						item.setDn_no(iaryC.getDn_no());
						item.setSht_ope_msg(inTrx.getRemark());
						//item.setPallet_id(item.getPallet_id());
                        retPrdInfoDao.update(item);
					}

    				ret_ship_notice.setWh_out_prd_qty(ret_ship_notice.getWh_out_prd_qty() + prdInfoList.size());
    				if (ret_ship_notice.getWh_out_prd_qty().equals(ret_ship_notice.getShip_qty())) {
    					ret_ship_notice.setDn_stat(PROC_COMPLETE);
    				}
    				ret_ship_notice.setShip_usr(inTrx.getRelate_usr());
    				ret_ship_notice.setShip_car_id(inTrx.getPalte_no());
    				ret_ship_notice.setEvt_timestamp(gEvtTimestamp);
    				ret_ship_notice.setShipping_timestamp(gEvtTimestamp);
    				ret_ship_notice.setEvt_cate(EVT_WHOUT);
                    retShipNoticeDao.update(ret_ship_notice);
        			ret_wo.setWh_out_prd_qty(ret_wo.getWh_out_prd_qty() + prdInfoList.size());
        			// Update for wip in
        			if (retBoxDao.isInWipDestShop(box)) {
        				ret_wo.setWip_wh_in_prd_qty(ret_wo.getWip_wh_in_prd_qty() - prdInfoList.size());
        				ret_wo.setWip_wh_out_prd_qty(ret_wo.getWip_wh_out_prd_qty() + prdInfoList.size());
        			} else {// 仅当BOX为成品时才扣减此数量
        				ret_wo.setWh_in_prd_qty(ret_wo.getWh_in_prd_qty() - prdInfoList.size());
        			}
        			ret_wo.setEvt_timestamp(gEvtTimestamp);
        			ret_wo.setEvt_usr(inTrx.getEvt_user());
        			retWODao.colseWo(ret_wo);
        			break;
				}else if(prdInfoList.size()>dnleftcnt){
					for (int i = 0; i < dnleftcnt; i++) {
						Ret_prd_info ret_prd_info = prdInfoList.get(i);
						if (!PROC_SHIP.equals(ret_prd_info.getPrd_stat())) {
							gszMsgBuf = "出货的玻璃状态必须是 SHIP, ["
									+ ret_prd_info.getPrd_seq_id() + "] 当前状态是 ["
									+ ret_prd_info.getPrd_stat() + "]";
							return INVALIED_SHT_STAT;
						}
						ret_prd_info.setEvt_cate(EVT_WHOUT);
						ret_prd_info.setPrd_stat(PROC_SHTC);
						ret_prd_info.setEvt_usr(inTrx.getEvt_user());
						ret_prd_info.setEvt_timestamp(gEvtTimestamp);
						ret_prd_info.setEvt_time_d(gEvtTimeD);
						ret_prd_info.setShipping_timestamp(gEvtTimestamp);
						ret_prd_info.setShip_box_id_fk(box.getShip_box_id());
						ret_prd_info.setDn_no(iaryC.getDn_no());
						ret_prd_info.setSht_ope_msg(inTrx.getRemark());
						//ret_prd_info.setPallet_id(ret_prd_info.getPallet_id());
                        retPrdInfoDao.update(ret_prd_info);
					}
    				ret_ship_notice.setWh_out_prd_qty(ret_ship_notice.getShip_qty());
    				ret_ship_notice.setDn_stat(PROC_COMPLETE);
    				ret_ship_notice.setShip_usr(inTrx.getRelate_usr());
    				ret_ship_notice.setShip_car_id(inTrx.getPalte_no());
    				ret_ship_notice.setEvt_timestamp(gEvtTimestamp);
    				ret_ship_notice.setShipping_timestamp(gEvtTimestamp);
    				ret_ship_notice.setEvt_cate(EVT_WHOUT);
                    retShipNoticeDao.update(ret_ship_notice);
        			ret_wo.setWh_out_prd_qty(ret_wo.getWh_out_prd_qty() + dnleftcnt);
        			// Update for wip in
        			if (retBoxDao.isInWipDestShop(box)) {
        				ret_wo.setWip_wh_in_prd_qty(ret_wo.getWip_wh_in_prd_qty() - dnleftcnt);
        				ret_wo.setWip_wh_out_prd_qty(ret_wo.getWip_wh_out_prd_qty() + dnleftcnt);
        			} else {// 仅当BOX为成品时才扣减此数量
        				ret_wo.setWh_in_prd_qty(ret_wo.getWh_in_prd_qty() - dnleftcnt);
        			}
        			ret_wo.setEvt_timestamp(gEvtTimestamp);
        			ret_wo.setEvt_usr(inTrx.getEvt_user());
        			retWODao.colseWo(ret_wo);
				}else{
					gszMsgBuf = "Count ERR!";
					return INVALIED_SHT_STAT;
				}
			}
			box.setBox_stat(PROC_SHTC);
			box.setShipping_timestamp(gEvtTimestamp);
			box.setRelate_usr(inTrx.getRelate_usr());
            box.setEvt_cate(EVT_WHOUT);
			box.setEvt_usr(inTrx.getEvt_user());
			box.setEvt_timestamp(gEvtTimestamp);
			box.setEvt_time_d(gEvtTimeD);
            retBoxDao.update(box);

			prdInfoList = new ArrayList<Ret_prd_info>();
			String hqlsht = "FROM Ret_prd_info where box_id_fk = ? ";
			prdInfoList = retPrdInfoDao.list(hqlsht, iary.getBox_id());
			
			rtnBeen = addTableForSHTC(box, prdInfoList,ret_wo);
			if (_NORMAL != rtnBeen.getlRc()) {
				gszMsgBuf = rtnBeen.getMsgBuf();
				return rtnBeen.getlRc();
			}
		}
		
		/**
		 * 栈板不为空时导出栈板信息
		 * 检查出账箱号group id是否相同
		 */
//		if(!Util.isSpaceCheck(inTrx.getPallet_id()) && _YES.equals(ret_wo.getSwh_cnt_flg())){
//			rtnBeen = checkMix();
//			if(_NORMAL != rtnBeen.getlRc()){
//				gszMsgBuf = rtnBeen.getMsgBuf();
//				return rtnBeen.getlRc();
//			}
//			outTrx.setExport_flg("Y");
//		}else{
//			outTrx.setExport_flg("N");
//		}

		if( _YES.equals(ret_wo.getSwh_cnt_flg())){
//			rtnBeen = checkMix();
//			if(_NORMAL != rtnBeen.getlRc()){
//				gszMsgBuf = rtnBeen.getMsgBuf();
//				return rtnBeen.getlRc();
//			}

			if("007".equals(ret_wo.getCus_id_fk()) && !palletIdSet.isEmpty()){
				for (String palletId : palletIdSet) {
					sendToCsotFtpProc(palletId);
				}
			}
			outTrx.setExport_flg("Y");
		}else{
			outTrx.setExport_flg("N");
		}
		return lRc;
	}
	
	private long whOutbywo() throws ParseException {
		long lRc = _NORMAL;
		SimpleRtnBeen rtnBeen = new SimpleRtnBeen(_NORMAL);
		Ret_box box;
		Ret_wo ret_wo = new Ret_wo();
		Ret_ship_notice ret_ship_notice;
		List<Ret_prd_info> prdInfoList ;
		Set<String> palletIdSet = new HashSet<String>();
		lRc = checkBoxPallet();
		if (lRc != _NORMAL) {
			return lRc;
		}
		if (inTrx.getBox_cnt() > 0) {

			for (XpwmsopeIA iary : inTrx.getIary()) {
				// Get box
				box = new Ret_box();
				box = retBoxDao.getWithLock(iary.getBox_id(), LockMode.UPGRADE);
				if (null == box) {
					gszMsgBuf = "箱子[" + iary.getBox_id() + "] 不存在 ["
							+ Ret_box.class.getSimpleName().toUpperCase()
							+ "]!";
					return E_RET_BOX + E_READ_NOT_FOUND;
				}
				palletIdSet.add(box.getPallet_id_fk());

				// Check box info
				if (!PROC_SHIP.equals(box.getBox_stat())) {
					gszMsgBuf = "出货箱子状态必须是 SHIP, [" + box.getBox_id() + "] 是 ["
							+ box.getBox_stat() + "]";
					return INVALIED_BOX_STAT;
				}
				for (XpwmsopeIC iaryC : inTrx.getIaryC()) {
					Integer dnleftcnt = 0;
					ret_ship_notice = new Ret_ship_notice();
					String hql = "FROM Ret_ship_notice where evt_seq_id=?";
					ret_ship_notice = retShipNoticeDao.uniqueResultWithLock(
							hql, iaryC.getEvt_seq_id());
					if (null == ret_ship_notice) {
						gszMsgBuf = new NotFoundRtnBeen(iaryC.getDn_no(),
								Ret_ship_notice.class).toString();
						return E_RET_SHIP_NOTICE + E_READ_NOT_FOUND;
					}
					dnleftcnt = ret_ship_notice.getShip_qty()
							- ret_ship_notice.getWh_out_prd_qty();
					if (box.getWo_id_fk().equals(ret_ship_notice.getWo_id_fk())) {
						if (box.getPrd_qty() > dnleftcnt) {
							gszMsgBuf = "箱子数量超过本次交货订单允许的数量!";
							return E_XPWMSOPE_DN_NOT_ENOUGH;
						}
						if (!PROC_WAIT.equals(ret_ship_notice.getDn_stat())) {
							gszMsgBuf = "交货订单 [" + ret_ship_notice.getDn_no()
									+ "] 状态 [" + ret_ship_notice.getDn_stat()
									+ "] 异常，必须是 [" + PROC_WAIT + "]， 请确认!";
							return E_XPWMSOPE_SHIP_NOTICE_NOT_WAIT;
						}

						ret_ship_notice.setWh_out_prd_qty(ret_ship_notice
								.getWh_out_prd_qty() + box.getPrd_qty());
						if (ret_ship_notice.getWh_out_prd_qty().equals(
								ret_ship_notice.getShip_qty())) {
							ret_ship_notice.setDn_stat(PROC_COMPLETE);
						}
						ret_ship_notice.setShip_usr(inTrx.getRelate_usr());
						ret_ship_notice.setShip_car_id(inTrx.getPalte_no());
						ret_ship_notice.setEvt_timestamp(gEvtTimestamp);
						ret_ship_notice.setShipping_timestamp(gEvtTimestamp);
						ret_ship_notice.setEvt_cate(EVT_WHOUT);
						retShipNoticeDao.update(ret_ship_notice);
						// WO wh_out_cnt check
						ret_wo = new Ret_wo();
						ret_wo = retWODao.getWithLock(box.getWo_id_fk(),
								LockMode.UPGRADE);
						if (null == ret_wo) {
							gszMsgBuf = new NotFoundRtnBeen(box.getWo_id_fk(),
									Ret_wo.class).toString();
							return E_RET_WO + E_READ_NOT_FOUND;
						}
						ret_wo.setWh_out_prd_qty(ret_wo.getWh_out_prd_qty()
								+ box.getPrd_qty());
						// Update for wip in
						if (retBoxDao.isInWipDestShop(box)) {
							ret_wo.setWip_wh_in_prd_qty(ret_wo
									.getWip_wh_in_prd_qty() - box.getPrd_qty());
							ret_wo.setWip_wh_out_prd_qty(ret_wo
									.getWip_wh_out_prd_qty() + box.getPrd_qty());
						} else {// 仅当BOX为成品时才扣减此数量
							ret_wo.setWh_in_prd_qty(ret_wo.getWh_in_prd_qty()
									- box.getPrd_qty());
						}

						ret_wo.setEvt_timestamp(gEvtTimestamp);
						ret_wo.setEvt_usr(inTrx.getEvt_user());
						retWODao.colseWo(ret_wo);

						// Fetch panels
						prdInfoList = new ArrayList<Ret_prd_info>();
						String hqlsht = "FROM Ret_prd_info where box_id_fk = ?";
						prdInfoList = retPrdInfoDao.listWithLock(hqlsht,
								box.getBox_id());
						if (!prdInfoList.isEmpty() && null != prdInfoList) {
							if (box.getPrd_qty() == prdInfoList.size()) {
								for (Ret_prd_info item : prdInfoList) {
									if (!PROC_SHIP.equals(item.getPrd_stat())) {
										gszMsgBuf = "出货的玻璃状态必须是 SHIP, ["
												+ item.getPrd_seq_id()
												+ "] 当前状态是 ["
												+ item.getPrd_stat() + "]";
										return INVALIED_SHT_STAT;
									}

									item.setEvt_cate(EVT_WHOUT);
									item.setPrd_stat(PROC_SHTC);
									item.setEvt_usr(inTrx.getEvt_user());
									item.setEvt_timestamp(gEvtTimestamp);
									item.setEvt_time_d(gEvtTimeD);
									item.setShipping_timestamp(gEvtTimestamp);
									item.setShip_box_id_fk(box.getShip_box_id());
									item.setDn_no(ret_ship_notice.getDn_no());
									item.setSht_ope_msg(inTrx.getRemark());
									//item.setPallet_id(item.getPallet_id());
									retPrdInfoDao.update(item);
								}

								String prd_gradeNG = prdInfoList.get(0)
										.getPrd_grade();
								// 原箱check
								// 非OK品不做此check -- 2015.01.26
//								if (_OK.equals(prd_gradeNG)) {
//									if (_YES.equals(ret_wo.getOpe_eff_flg())) {
//										rtnBeen = retBoxDao.chkMtrlBoxAndSlot(
//												box, true);
//										if (_NORMAL != rtnBeen.getlRc()) {
//											gszMsgBuf = rtnBeen.getMsgBuf();
//											return rtnBeen.getlRc();
//										}
//									}
//								}
							} else {
								gszMsgBuf = "Box [" + box.getBox_id()
										+ "] has [" + box.getPrd_qty()
										+ "] panels, but only find ["
										+ prdInfoList.size() + "].";
								return INVALIED_SHT_CNT;
							}
						} else {
							gszMsgBuf = "No panel in box [" + box.getBox_id()
									+ "].";
							return E_XPWMSOPE_FIND_NO_PANEL_BY_BOX;
						}
						box.setBox_stat(PROC_SHTC);
						box.setShipping_timestamp(gEvtTimestamp);
						box.setRelate_usr(inTrx.getRelate_usr());
						box.setEvt_cate(EVT_WHOUT);
						box.setEvt_usr(inTrx.getEvt_user());
						box.setEvt_timestamp(gEvtTimestamp);
						box.setEvt_time_d(gEvtTimeD);
						retBoxDao.update(box);

						/**
						 * add RET_PRD_INFO_SHTC & RET_BOX_SHTC for RMA
						 */
						rtnBeen = addTableForSHTC(box, prdInfoList, ret_wo);
						if (_NORMAL != rtnBeen.getlRc()) {
							gszMsgBuf = rtnBeen.getMsgBuf();
							return rtnBeen.getlRc();
						}
					}
				}
			}
			/**
			 * 栈板不为空时导出栈板信息
			 * 检查出账箱号group id是否相同
			 */

			if( _YES.equals(ret_wo.getSwh_cnt_flg())){
//				rtnBeen = checkMix();
//				if(_NORMAL != rtnBeen.getlRc()){
//					gszMsgBuf = rtnBeen.getMsgBuf();
//					return rtnBeen.getlRc();
//				}
				if("007".equals(ret_wo.getCus_id_fk()) && !palletIdSet.isEmpty()){
					for (String palletId : palletIdSet) {
						sendToCsotFtpProc(palletId);
					}
				}
				outTrx.setExport_flg("Y");
			}else{
				outTrx.setExport_flg("N");
			}

//			if(!Util.isSpaceCheck(inTrx.getPallet_id()) && _YES.equals(ret_wo.getSwh_cnt_flg())){
//				rtnBeen = checkMix();
//				if(_NORMAL != rtnBeen.getlRc()){
//					gszMsgBuf = rtnBeen.getMsgBuf();
//					return rtnBeen.getlRc();
//				}
//				outTrx.setExport_flg("Y");
//			}else{
//				outTrx.setExport_flg("N");
//			}
		}
		return lRc;
	}
	
	private void sendGradeRatioFunc(String cus_id ,String wo_id ,
			String so_id ,String act_stb_time ,Integer pln_prd_qty){
		//留滞率
		AmslzgradeI amslzgradeI = new AmslzgradeI();
		amslzgradeI.setTrx_id(T_AMSLZGRADE);
		amslzgradeI.setType_id(TRX_IN);
		amslzgradeI.setCus_id(cus_id);
		amslzgradeI.setWo_id(wo_id);
		amslzgradeI.setSo_id(so_id);
		amslzgradeI.setAct_stb_time(act_stb_time);
		amslzgradeI.setPln_prd_qty(pln_prd_qty);
		String strLzInMsg = mqOpe.obj2Str(amslzgradeI);
		sendMsgService.sendWithRepaly(strLzInMsg);
		//订单不良率
		AmsnggradeI amsnggradeI = new AmsnggradeI();
		amsnggradeI.setTrx_id(T_AMSNGGRADE);
		amsnggradeI.setType_id(TRX_IN);
		amsnggradeI.setCus_id(cus_id);
		amsnggradeI.setWo_id(wo_id);
		amsnggradeI.setSo_id(so_id);
		amsnggradeI.setAct_stb_time(act_stb_time);
		amsnggradeI.setPln_prd_qty(pln_prd_qty);
		String strNgInMsg = mqOpe.obj2Str(amsnggradeI);
		sendMsgService.sendWithRepaly(strNgInMsg);
	}
	
	private long exportToFtpProc(){
		long lRc = _NORMAL;
		logger.info(inTrx.getPallet_id());
		String strOutTrx = httpClientUtil.postHttp(inTrx.getPallet_id());
		logger.info(strOutTrx);
		CsotShippingO csotOut = new CsotShippingO();
		csotOut = (CsotShippingO) mqOpe.inStr2Obj(strOutTrx, csotOut);
		if(!"0000000".equals(csotOut.getRtn_code()) ){
			gszMsgBuf = "WG资料外发失败: \n" + csotOut.getRtn_mesg();
			return 9999999;
		}
		return lRc;
	}
	
	/**
	 * GROUP ID,针对客户来料的订单，
	 * 不同group id(值来自RET_CUS_PRD_IN的CUS_GROUP_ID,
	 * 即owner id)的产品不能混装
	 * @return
	 */
	private SimpleRtnBeen checkMix(){
		SimpleRtnBeen rtnBeen = new SimpleRtnBeen(_NORMAL);
		String sql = "FROM Ret_prd_info WHERE pallet_id = '" + inTrx.getPallet_id() + "'";
		List<Ret_prd_info> ret_prd_info = retPrdInfoDao.find(sql);
		for(int i=0; i<ret_prd_info.size(); i++){
			if(!ret_prd_info.get(0).getGroup_id().equals(ret_prd_info.get(i).getGroup_id())){
				gszMsgBuf = "玻璃的组代码不能混！[" + ret_prd_info.get(0).getPrd_seq_id()+ "]["
					+ ret_prd_info.get(0).getGroup_id() + "] vs ["
					+ ret_prd_info.get(i).getPrd_seq_id() + "]["
					+ ret_prd_info.get(i).getGroup_id() + "]";
				rtnBeen.setlRc(GROUP_ID_MIX);
				rtnBeen.setMsgBuf(gszMsgBuf);
			}
			
			if(!ret_prd_info.get(0).getPrd_grade().equals(ret_prd_info.get(i).getPrd_grade())){
				gszMsgBuf = "玻璃的等级不能混！[" + ret_prd_info.get(0).getPrd_seq_id()+ "]["
					+ ret_prd_info.get(0).getPrd_grade() + "] vs ["
					+ ret_prd_info.get(i).getPrd_seq_id() + "]["
					+ ret_prd_info.get(i).getPrd_grade() + "]";
				rtnBeen.setlRc(PRD_GRADE_MIX);
				rtnBeen.setMsgBuf(gszMsgBuf);
			}
		}
		return rtnBeen;
	}
	
	/**
	 * pack box for whout
	 * by wfq
	 * @return
	 */
	private long packBox() {
		long lRc = _NORMAL;
		int iSerial = 0;
		Ret_crt_noId id = new Ret_crt_noId();
		Ret_box ret_box = retBoxDao.getWithLock(inTrx.getIary().get(0).getBox_id(), LockMode.UPGRADE);
		List<Ret_prd_info> ret_prd_infos = retPrdInfoDao.getShtListInfo(inTrx.getIary().get(0).getBox_id());
		Ret_wo ret_wo = retWODao.get(ret_prd_infos.get(0).getWo_id_fk());
		String gMdl_id = ret_wo.getMdl_id_fk();
		//打包时根据规格生成栈板号
		String palletId = "";
		//第1、2码为生产厂区
		palletId = palletId + "BA";
		//第3、4码为WO中product ID的末两位
		palletId = palletId + gMdl_id.substring(gMdl_id.length()-2, gMdl_id.length());
		//第5、6码为固定01
		palletId = palletId + "01";
		//第7、8码为固定00
		palletId = palletId + "00";
		//第9、10、11、12、13码为西元后两码、月份、日期
		palletId = palletId + Util.getYearMonthDayZero(2,"N");
		String key_id = Util.getYearMonthDayZero(2,"N");
		//第14~18码为流水码
		id.setKey_id(key_id);
		id.setType("P");
		Ret_crt_no ret_crt_no = retCrtNoDao.getWithLock(id,LockMode.UPGRADE);
		if(ret_crt_no==null){
			ret_crt_no = new Ret_crt_no();
			ret_crt_no.setId(id);
		}else{
			iSerial = Integer.valueOf(ret_crt_no.getSerial_no());
		}
		iSerial++;
		palletId = palletId + Util.comInt2String(iSerial, 5);
		ret_crt_no.setSerial_no(String.valueOf(iSerial));
		retCrtNoDao.saveOrUpdate(ret_crt_no);

//		String palletId = inTrx.getPallet_id();
		String phypalletId = inTrx.getPhypallet_id();
		List<Ret_box> retBoxList = new ArrayList<>();
		if (inTrx.getBox_cnt() > 0) {
			for (int i = 0;i<inTrx.getBox_cnt();i++) {

				// Get box
				Ret_box box = new Ret_box();
				box = retBoxDao.getWithLock(inTrx.getIary().get(i).getBox_id(), LockMode.UPGRADE);
				if (null == box) {
					gszMsgBuf = "箱子 [" + inTrx.getIary().get(i).getBox_id() + "] 不存在 ["
							+ Ret_box.class.getSimpleName().toUpperCase()
							+ "]!";
					return E_RET_BOX + E_READ_NOT_FOUND;
				}
				
				// Fetch panels
				List<Ret_prd_info> prdInfoList = new ArrayList<Ret_prd_info>();
				prdInfoList = retPrdInfoDao.getShtListInfo(inTrx.getIary().get(i).getBox_id());
				if (null != prdInfoList) {
						for (Ret_prd_info item : prdInfoList) {
							item.setPallet_id(palletId);
							item.setExt_11(phypalletId);
							item.setEvt_cate("PKBK");
						    item.setEvt_usr(inTrx.getEvt_user());
							item.setEvt_timestamp(gEvtTimestamp);
							retPrdInfoDao.update(item);
						}
				} else {
					gszMsgBuf = "No panel in box [" + inTrx.getIary().get(i).getBox_id() + "].";
					return E_XPWMSOPE_FIND_NO_PANEL_BY_BOX;
				}
				box.setPallet_id_fk(palletId);
				box.setExt_11(phypalletId);
				box.setEvt_cate("PKBK");
				box.setEvt_usr(inTrx.getEvt_user());
				box.setEvt_timestamp(gEvtTimestamp);
				retBoxDao.update(box);
				retBoxList.add(box);
			}
		}
		outTrx.setOary(retBoxList);
		return lRc;
	}
	
	/**
	 * cancel pack box for whout
	 * by wfq
	 * @return
	 */
	private long unpackBox() {
		long lRc = _NORMAL;
		if (inTrx.getBox_cnt() > 0) {
			for (XpwmsopeIA iary : inTrx.getIary()) {

				// Get box
				Ret_box box = new Ret_box();
				box = retBoxDao.getWithLock(iary.getBox_id(), LockMode.UPGRADE);
				if (null == box) {
					gszMsgBuf = "箱子 [" + iary.getBox_id() + "] 不存在 ["
							+ Ret_box.class.getSimpleName().toUpperCase()
							+ "]!";
					return E_RET_BOX + E_READ_NOT_FOUND;
				}
				if(Util.isSpaceCheck(box.getPallet_id_fk())){
					gszMsgBuf = "箱子 [" + iary.getBox_id() + "]还未做打包操作!";
					return E_RET_BOX;
				}
				// Fetch panels
				List<Ret_prd_info> prdInfoList = new ArrayList<Ret_prd_info>();
				prdInfoList = retPrdInfoDao.getShtListInfo(iary.getBox_id());
				if (null != prdInfoList) {
						for (Ret_prd_info item : prdInfoList) {
							item.setPallet_id("");
							item.setExt_11("");
							item.setEvt_cate("UNBK");
						    item.setEvt_usr(inTrx.getEvt_user());
							item.setEvt_timestamp(gEvtTimestamp);
							retPrdInfoDao.update(item);
						}
				} else {
					gszMsgBuf = "No panel in box [" + iary.getBox_id() + "].";
					return E_XPWMSOPE_FIND_NO_PANEL_BY_BOX;
				}
				box.setPallet_id_fk("");
				box.setExt_11("");
				box.setEvt_cate("UNBK");
				box.setEvt_usr(inTrx.getEvt_user());
				box.setEvt_timestamp(gEvtTimestamp);
				retBoxDao.update(box);
			}
		}

		return lRc;
	}
	/**
	 * check box of pallet
	 * by wfq
	 * @return
	 */
	private long checkBoxPallet(){
		long lRc = _NORMAL;
		Map<String, Integer> PalletBoxMap = new HashMap<String, Integer>();
		for (XpwmsopeIA iary : inTrx.getIary()) {
			Ret_box box = new Ret_box();
			box = retBoxDao.getWithLock(iary.getBox_id(), LockMode.UPGRADE);
			if (null == box) {
				gszMsgBuf = "箱子[" + iary.getBox_id() + "] 不存在 ["
						+ Ret_box.class.getSimpleName().toUpperCase()
						+ "]!";
				return E_RET_BOX + E_READ_NOT_FOUND;
			}
			if (PalletBoxMap.containsKey(box.getPallet_id_fk())) {
				PalletBoxMap.put(box.getPallet_id_fk(), PalletBoxMap.get(box.getPallet_id_fk()) + 1);
			} else {
				PalletBoxMap.put(box.getPallet_id_fk(), 1);
			}
		}
		Iterator<Entry<String, Integer>> ite = PalletBoxMap.entrySet()
				.iterator();
		while (ite.hasNext()) {
			Entry<String, Integer> pb = ite.next();
			int cnt = retBoxDao.getPalletBoxCnt(pb.getKey());
			if(cnt!=pb.getValue()){
				gszMsgBuf = "栈板[" + pb.getKey() + "]包含箱子[" + cnt + "]个，必须同时出货";
				return E_RET_BOX + E_READ_NOT_FOUND;
			}
		}
		return lRc;
	}
	
	private long sendXmtmShippingDataProc(String pallet_id) {
		long lRc = _NORMAL;
		String plant_id;
		String strOutTrx;
		logger.info(inTrx.getPallet_id());
		
		plant_id="";
		strOutTrx="";
		List<Ret_box> ret_boxList = new ArrayList<Ret_box>();

		String hqlQuery = "FROM Ret_box where 1=1 ";
		if (!Util.isSpaceCheck(inTrx.getPallet_id())) {
			hqlQuery += " AND pallet_id_fk = '" + pallet_id + "' ";
		}
		ret_boxList = retBoxDao.find(hqlQuery);
		if (!ret_boxList.isEmpty()) {
			Ret_box box= ret_boxList.get(0);
			Ret_wo ret_wo = retWODao.get(box.getWo_id_fk());
			if(ret_wo!= null)
			{
				plant_id = ret_wo.getPlant_id();
			}
		}
		if("G5".equals(plant_id)){
			 strOutTrx= xmtm5HttpClientUtil.postHttp(inTrx.getPallet_id());
		}else{
			 strOutTrx = xmtmHttpClientUtil.postHttp(inTrx.getPallet_id());
		}
		logger.info(strOutTrx);
		CsotShippingO csotOut = new CsotShippingO();
		csotOut = (CsotShippingO) mqOpe.inStr2Obj(strOutTrx, csotOut);
		if(!"0000000".equals(csotOut.getRtn_code()) ){
			gszMsgBuf = "WG资料外发失败: \n" + csotOut.getRtn_mesg();
			return 9999999;
		}
		return lRc;
		
	}
	
	private long reExportXmtmProc() {
		long lRc = _NORMAL;
		String plant_id;
		String strOutTrx;
		logger.info(inTrx.getPallet_id());
		
		plant_id="";
		strOutTrx="";
		List<Ret_box> ret_boxList = new ArrayList<Ret_box>();

		String hqlQuery = "FROM Ret_box where 1=1 ";
		if (!Util.isSpaceCheck(inTrx.getPallet_id())) {
			hqlQuery += " AND pallet_id_fk = '" + inTrx.getPallet_id() + "' ";
		}
		ret_boxList = retBoxDao.find(hqlQuery);
		if (!ret_boxList.isEmpty()) {
			Ret_box box= ret_boxList.get(0);
			Ret_wo ret_wo = retWODao.get(box.getWo_id_fk());
			if(ret_wo!= null)
			{
				plant_id = ret_wo.getPlant_id();
			}
		}
		if("G5".equals(plant_id)){
			 strOutTrx= xmtm5HttpClientUtil.postHttp(inTrx.getPallet_id());
		}else{
			 strOutTrx = xmtmHttpClientUtil.postHttp(inTrx.getPallet_id());
		}
		logger.info(strOutTrx);
		CsotShippingO csotOut = new CsotShippingO();
		csotOut = (CsotShippingO) mqOpe.inStr2Obj(strOutTrx, csotOut);
		if(!"0000000".equals(csotOut.getRtn_code()) ){
			gszMsgBuf = "WG资料外发失败: \n" + csotOut.getRtn_mesg();
			return 9999999;
		}
		return lRc;
	}

	private long reExportToCsotFtpProc(){
		logger.info(inTrx.getPallet_id());
		return sendToCsotFtpProc(inTrx.getPallet_id());

	}

	private long sendToCsotFtpProc(String palletId){
		String strOutTrx = csotHttpClientUtil.postHttp(palletId);
		logger.info(strOutTrx);
		CsotShippingO csotOut = new CsotShippingO();
		csotOut = (CsotShippingO) mqOpe.inStr2Obj(strOutTrx, csotOut);
		if(!"0000000".equals(csotOut.getRtn_code()) ){
			gszMsgBuf = "资料外发失败: \n" + csotOut.getRtn_mesg();
			return 9999999;
		}
		return _NORMAL;
	}

}
